Inheritance in Java
Inheritance is a mechanism of inheriting properties and behaviors (fields and methods) in one class from another class. A class which is inherited from another class is called a subclass, extended class, derived class or child class of the class from which it is inherited.
The class from which the subclass is inherited is called its parent class or super class or base class. In Java, every class is a subclass and descendants of Object class defined in the java.lang package.
Syntax
class ParentClass {
}
class SubClass extends ParentClass {
}
Example
Here is an example of Animal parent/super class:
public class Animal {
public String name;
public void eat() {
System.out.println(name + " can eat");
}
public void sleep() {
System.out.println(name + " can sleep");
}
}
Here is an example of Dog class which is a subclass of Animal class:
public class Dog extends Animal {
public Dog(String name) {
this.name = name;
}
public void swim() {
System.out.println(name + " can swim");
}
public static void main(String[] args) {
Dog dog = new Dog("Dog");
dog.eat();
dog.sleep();
dog.swim();
}
}
Output
Dog can sleep
Dog can swim
Here, Dog class inherits all members(field and methods) of the Animal class. The Dog class is created as if from scratch with one field and three methods without having to rewrite everything that had already been there in Animal class.
When to Use Inheritance
Use inheritance, when you want to acquire all members(fields and methods) of one class in another class.
Constructors are not considered as members of a super class and therefore, they are not inherited in a subclass. However, constructors of a super class can be invoked from its subclasses.
A list of important things to remember while using inheritance:
- All fields and methods declared as public and protected are inherited in a subclass from its super class, even if the subclasses are in different packages.
- The inherited field can be used directly or with modifications in a subclass.
- A new instance method can be created in a subclass that has exactly the same method's name, return-type and arguments as in the parent class. In other words, it is overriding of methods of the superclass in its subclass.
- Constructors of the superclass can be invoked from a subclass's constructor using super keyword.
- A new static method can be created in a subclass that has exactly the same method's name, return-type and arguments as in the superclass. In other words, it is hiding of methods of the superclass in its subclass.
- New fields and methods, which are not in the superclass can be created in its subclasses.
- A new field with the same name and return-type as the one in the superclass, can be created in its subclass. This will hide the field of the superclass and thus not recommended.
- A subclass cannot inherit a private method of a superclass. However, if a superclass has public or protected methods that provides access to its private fields then these methods can be used by its subclasses to access those private fields of the superclass.
- A nested class can access all private fields and methods of the enclosing class. This makes private fields and methods of the superclass indirectly accessible from the inherited public or protected nested class in a subclass.