What is Is-a relationship in Java?


IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

Example

public class Animal {
}
public class Mammal extends Animal {
}
public class Reptile extends Animal {
}
public class Dog extends Mammal {
}

Now, based on the above example, in Object-Oriented terms, the following are true −

  • Animal is the superclass of Mammal class.
  • Animal is the superclass of Reptile class.
  • Mammal and Reptile are subclasses of Animal class.
  • Dog is the subclass of both Mammal and Animal classes.

Example

 Live Demo

class Animal {
}
class Mammal extends Animal {
}
class Reptile extends Animal {
}
public class Dog extends Mammal {
public static void main(String args[]) {
Animal a = new Animal();
Mammal m = new Mammal();
Dog d = new Dog();

System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}

Output

true
true
true

Updated on: 30-Jul-2019

912 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements