Inheritance allows one class (child or subclass) to use the properties and methods of its parent class (superclass). This greatly enhances code reusability and helps to avoid redundancy.
Note: : Java allows single inheritance, meaning that a class can only inherit from one parent class at a time.
public class Animal {
void eat() {
System.out.println("This animal eats");
}
}
public class Dog extends Animal {
void bark() {
System.out.println("This dog barks");
}
}
public class InheritanceExample {
public static void main(String[] args){
Dog myDog = new Dog();
myDog.eat(); // This animal eats
myDog.bark(); // This dog barks
}
}
In the code above, we can see that the Dog class can use both the bark() method and eat() method because it is the child of the Animal class.
public class Animal {
private int age; // Not inherited by child classes
protected String breed; // Inherited if in same package or subclass
}
public class Dog extends Animal {
public void show() {
// System.out.println(age); // Error. private field
System.out.println(breed); // Works. protected field
}
}
super() KeywordThe super keyword is used to refer to the parent class. It can be used for:
1. Calling the parent class's constructor.
2. Accessing the parent class's methods or fields when they are overridden in the child class.
The child class can use super() to call the parent class’s constructor. It must be the first line inside the child class constructor. If super() isn't called explicitly, the compiler will assume that the parent class's no-argument constructor is being called.
public class Animal {
String breed;
public Animal() {
System.out.println("Animal class constructor");
}
public Animal(String breed) {
this.breed = breed;
}
}
public class Dog extends Animal {
public Dog() {
super(); // Calls Animal's no-argument constructor
}
public Dog(String breed) {
super(breed); // Calls Animal's parameterized constructor
}
}
If a class requires specific parameters for its constructor, you need to define it manually in the child class. If not, the compiler tries to use the default no-argument constructor. Not providing an appropriate constructor when parameters are required will result in a compilation error.
Method overriding allows the child class to provide its own implementation of a method that is already defined in the parent class. This is useful when the subclass needs to modify or extend the behavior of the parent class's method.
public class Animal {
public void sound() {
System.out.println("Animal makes a cry!");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("Woof!");
}
}
Dog overrides the sound() method from Animal. Instead of printing "Animal makes a cry!" the Dog class will print "Woof!".
superWhen a method is overridden, the child class’s version of the method is called by default. However, you can still access the parent class’s version of the method using super.methodName().
super in Overriding:public class Animal {
public void eat() {
System.out.println("Animal is eating.");
}
}
public class Dog extends Animal {
@Override
public void eat() {
super.eat(); // Calls the parent class's eat() method
System.out.println("Dog is eating dog food.");
}
}
When the parent and child classes are in different packages:
// Parent class in package1
package package1;
public class Animal {
protected String color;
}
// Child class in package2
package package2;
import package1.Animal;
public class Dog extends Animal {
public void showColor() {
System.out.println(color); // Accessible because color is protected
}
}