Java Inheritance

The class which inherits the properties of others is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

extends Keyword

The extends is a Java keyword, which is used in inheritance process of Java. It is a keyword that indicates the parent class that a subclass is inheriting from and may not be used as identifiers.

class Super {
   .....
   .....
}
class Sub extends Super {
   .....
   .....
}

Sample Code

Using extends keyword, the My_Calculation inherits the methods addition() and Subtraction() of Calculation class.

class Calculation {
   int z;
 
   public void addition(int x, int y) {
      z = x + y;
      System.out.println("The sum of the given numbers:"+z);
   }
 
   public void Subtraction(int x, int y) {
      z = x - y;
      System.out.println("The difference between the given numbers:"+z);
   }
}

public class My_Calculation extends Calculation {
   public void multiplication(int x, int y) {
      z = x * y;
      System.out.println("The product of the given numbers:"+z);
   }
 
   public static void main(String args[]) {
      int a = 58, b = 20;
      My_Calculation demo = new My_Calculation();
      demo.addition(a, b);
      demo.Subtraction(a, b);
      demo.multiplication(a, b);
   }
}

Output

The sum of the given numbers:78
The difference between the given numbers:38
The product of the given numbers:1160

The super keyword

Consider the example shown below:

class Super_class {
   int num = 85;

   // display method of superclass
   public void display() {
      System.out.println("This is the display method of superclass");
   }
}

public class Sub_class extends Super_class {
   int num = 28;

   // display method of sub class
   public void display() {
      System.out.println("This is the display method of subclass");
   }

   public void my_method() {
      // Instantiating subclass
      Sub_class sub = new Sub_class();

      // Invoking the display() method of sub class
      sub.display();

      // Invoking the display() method of superclass
      super.display();

      // printing the value of variable num of subclass
      System.out.println("value of the variable named num in sub class:"+ sub.num);

      // printing the value of variable num of superclass
      System.out.println("value of the variable named num in super class:"+ super.num);
   }

   public static void main(String args[]) {
      Sub_class obj = new Sub_class();
      obj.my_method();
   }
}

Output

value of the variable named num in sub class:28
value of the variable named num in super class:85

Invoking Superclass Constructor

If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass.

class Superclass {
   int age;

   Superclass(int age) {
      this.age = age;    
   }

   public void getAge() {
      System.out.println("The value of the variable named age in super class is: " +age);
   }
}

public class Subclass extends Superclass {
   Subclass(int age) {
      super(age);
   }

   public static void main(String argd[]) {
      Subclass s = new Subclass(18);
      s.getAge();
   }
}

Output

The value of the variable named age in super class is: 18
IS-A is a way of saying: This object is a type of that object

IS-A Relationship

Consider the example shown below:

public class Animal {
}

public class Mammal extends Animal {
}

public class Reptile extends Animal {
}

public class Cat extends Mammal {
}
  • Animal is the superclass of Mammal class.
  • Animal is the superclass of Reptile class.
  • Mammal and Reptile are subclasses of Animal class.
  • Cat is the subclass of both Mammal and Animal classes.

IS-A relationship, we can say:

  • Mammal IS-A Animal
  • Reptile IS-A Animal
  • Dog IS-A Mammal
  • Hence: Cat IS-A Animal as well

The instanceof Keyword

instanceof operator to check determine whether Mammal is actually an Animal, and cat is actually an Animal.

class Superclass {
   int age;

   Superclass(int age) {
      this.age = age;    
   }

   public void getAge() {
      System.out.println("The value of the variable named age in super class is: " +age);
   }
}

public class Subclass extends Superclass {
   Subclass(int age) {
      super(age);

Output

true
true
true

HAS-A relationship

HAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.

public class Vehicle{}
public class Speed{}

public class Van extends Vehicle {
   private Speed sp;
} 
Related Post