top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Inheritance in Java

Inheritance is everywhere in java.We can say it is almost impossible to write a tiny piece of java code without using inheritance. Whenever we create a class, we automatically inherit all of the class Object’s method. Every class in java is a subclass of class Object. We can create inheritance relationship in java by extending a class. It’s also important to understand that the two most common reason s to use inheritance are:


  • To promote code reuse

  • To use polymorphism

Understanding Inheritance


When we design with inheritance, we put common code in a class and then tell other more specific classes that the common (more abstract) class is their superclass. When one class inherits from another, the subclass inherits from the superclass.

In Java, we say that the subclass extends the superclass. An inheritance relationship means that the subclass inherits the members of the superclass. When we say “members of a class” we mean the instance variables and methods.


Syntax to implement Inheritance is below:


class Subclass-name extends Superclass-name{//methods and fields}

Let’s Take an example in pictorial form to implement Inheritance:



We can read this as, “Circle inherits from Shape”, “Triangle inherits from Shape”, and so on. I removed rotate() and playSound() from the other shapes, so now there’s only one copy to maintain.

The Shape class is called the superclass of the other 2 classes. The other two are the subclasses of Shape. The subclasses inherit the methods of the superclass. In other words, if the Shape class has the functionality, then the subclasses automatically get that same functionality.

Let’s take one example to understand multi level inheritance



The Wolf class has 4 methods. One inherited from Animal,One inherited from Canine(Which is actually an overridden version of of a method of class Animal) and two overridden in the Wolf class. Overriding means a subclass redefines one of its inherited method when it needs to change or extends the behavior of that method.


Wolf w = new Wolf(); //make a new Wolf object
w.makeNoise(); //calls the version in Wolf
w.roam(); //calls the version in Canine
w.eat(); //calls the version in Wolf
w.sleep(); //calls the version in Animal

When we call a method of an object reference, we call most specific version of the method of that object type.In other words, Lowest one wins. “Lowest” means lowest on the inheritance tree.Canine is lower than Animal, Wolf is lower than Canine.So invoking a method on a reference to a Wolf object means JVM starts looking first in the Wolf class. If the JVM doesn’t find a version of the method in the Wolf class ,it starts walking back up the inheritance hierarchy until it finds a match.


IS-A and HAS-A Relationship


In OO, the concept of IS-A is based on class Inheritance or Interface implementation. IS-A is a way of saying, “this thing is a type of that thing”. For example, Triangle is a type of shape so in OO terms we can say , Triangle IS-A Shape , Wolf IS-A Canine.We express IS-A relationship in java through the keywords extends (for class inheritance) and implements (for interface implementation).


public class Animal{
//some code here
}
public class Canine extends Animal{
//some code here
}
public class Wolf extends Canine{
//some code here
}

In OO terms, we can say the following,

Animal is the superclass of Canine. Canine is the subclass of Animal. Canine is the super class of Wolf . Wolf is the subclass of Canine. Canine inherits from Animal. Wolf inherits from both Animal and Canine. etc.

HAS-A relationships are based on usage, rather than inheritance. In other words, class A HAS-A class B if code in class A has a reference to an instance of class B. Example, “Bathroom HAS-A Tub”. It means Bathroom has a Tub instance variable, but Bathroom does not extends Tub and vice-versa.


public class Bathroom{  
private Tub bathtub; //Has-A example
}
public class Tub{  
int size;  
Bubble b;//Has-A example
}
public class Bubble{
//code
}

Multiple Inheritance not supported in Java


To reduce the complexity and simplify the language Java multiple inheritance is not supported in Java.

Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.

Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.


class A{  
void msg(){
System.out.println("Hello world");}  
}  
class B{  
void msg(){
System.out.println("Welcome");
}  
}  
class C extends A,B{//suppose if it were      
public static void main(String args[]){     
C obj = new C();     
obj.msg(); //Now which msg() method would be invoked?  
}  
}  

In Summary,

Inheritance allows a class to be a subclass of a superclass, and thereby inherit public and protected variables and methods of the superclass.

Hope you find this blog helpful in learning Inheritance concept in Java.

Thank you and Happy Learning.


21 views0 comments

Recent Posts

See All
bottom of page