Inheritance allows a class to inherit fields and methods from another class, promoting code reuse.
Source Code
class Animal {
protected String name;
public void eat() {
System.out.println(name + " eats");
}
}
class Dog extends Animal {
Dog(String name) {
this.name = name;
}
public void display() {
System.out.println("My name is " + name);
}
}