IS-A Relationship

IS-A Relationship

In this tutorial, we will discuss IS-A relationship (also known as inheritance) in Java. In object-oriented programming, the concept of IS-A is based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying, “A is a B type of thing.”

For example, Apple is a Fruit, and Car is a Vehicle, etc. Inheritance is uni-directional. For example, House is a Building. But Building is not a House.

It is a key point to note that you can quickly identify the IS-A relationship. Wherever you see an extends keyword or implements keyword in a class declaration, then this class is said to have an IS-A relationship.

IS-A Relationship

Note

  • Parent class reference can be used to hold child class object but by using that reference we are not allowed to call child class specific methods.
class P {
   m1() {
   }
   m2() {
   }
}

class C extends P {
   m3() {
   }
}

class Test {
   public static void main(String arg[]) {
      P p1 = new P();
      p1.m1();
      p1.m2();
      p1.m3(); //C.E: Child class methods are not available to the parent class.

      C c1 = new C();
      c1.m1();
      c1.m2();
      c2.m3(); // Fine
   
      P p2 = new C();
      p2.m1();
      p2.m2();
      p2.m3(); //C.E: By using parent class reference we can’t call child class specific method
      
      C c4 = new P(); //C.E: Child class reference can’t be used to hold parent class object.
   }
}
class A extends B,C {
   -----------
   -----------
} // C.E

interface A extends B,C {

   -----------
   -----------
} // Fine
  • Cycle inheritance is not allowed in java
class A extends B {
}

class B extends A {
}

class A extends A {
}

 Above both gives compile time error saying cyclic inheritance involving.

That’s all about IS-A relationship in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!

IS-A Relationship
Scroll to top