What is inheritance in Java?
Answer
Inheritance means that one class can inherit (“extends”) another class. Thus, you can reuse the code from the class from which they are inherited. The existing class is known as superclass
and the one being created is known as subclass
. They also say parent
and child
.
public class Animal {
private int age;
}
public class Dog extends Animal {
}
where Animal
is parent
and Dog
– child
.