logo
logo
Sign in

what is inheritance in java and What are the types of inheritance in java

avatar
manoj sharma
what is inheritance in java and  What are the types of inheritance in java







Hierarchical Inheritance in Java


Inheritance is an important concept in Java programming language. It allows a class to inherit the properties and behavior of another class, which is known as the superclass or parent class. The class that inherits the properties and behavior is known as the subclass or child class.


In Java, hierarchical inheritance is a type of inheritance where multiple classes inherit from a single superclass. This means that a superclass can have more than one subclass. Each subclass inherits the properties and behavior of the superclass, and can also have its own unique properties and behavior.


This type of inheritance is useful in situations where there is a need for multiple classes to share common properties and behavior. By using hierarchical inheritance, code reuse and organization can be improved, as common functionality can be defined in the superclass and shared by all the subclasses.



Types of Inheritance in Java


Java supports several types of inheritance. Apart from hierarchical inheritance, the other types of inheritance are:



Single Inheritance


In single inheritance, a subclass inherits from a single superclass. This means that each subclass can have only one direct superclass. The subclass can inherit the properties and behavior of the superclass, and can also have its own unique properties and behavior.



Multiple Inheritance


Multiple inheritance is a type of inheritance where a subclass inherits from multiple superclasses. This means that each subclass can have multiple direct superclasses. However, Java does not support multiple inheritance of classes. It only supports multiple inheritance of interfaces, where a class can implement multiple interfaces.



Multilevel Inheritance


Multilevel inheritance is a type of inheritance where a subclass inherits from a superclass, and that subclass becomes the superclass for another subclass. This creates a chain of inheritance, where each subclass inherits from its immediate superclass, and so on.



Hybrid Inheritance


Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. It involves multiple superclasses and multiple levels of inheritance. However, as mentioned earlier, Java only supports multiple inheritance of interfaces and not classes.



Java Inheritance


In Java, inheritance is implemented using the "extends" keyword. The subclass is declared using the "class" keyword, followed by the name of the subclass and the "extends" keyword, followed by the name of the superclass. Here is an example:




public class SubClass extends SuperClass {
  // Subclass-specific properties and methods
}



In the above example, the subclass "SubClass" is inheriting from the superclass "SuperClass". The subclass can access all the public and protected properties and methods of the superclass, and can also override them to provide its own implementation.



Inheritance Example in Java


Let's consider an example to better understand inheritance in Java.


Suppose we have a superclass called "Vehicle", which has properties like "brand" and "year". It also has a method called "start()", which prints a message indicating that the vehicle has started.


We can create subclasses like "Car" and "Motorcycle" that inherit from the "Vehicle" superclass. The "Car" subclass can have additional properties like "numDoors" and "color", as well as its own implementations of methods like "start()" and "stop()". The "Motorcycle" subclass can have additional properties like "hasHelmet" and "topSpeed", as well as its own implementations of methods.


By inheriting from the "Vehicle" superclass, the subclasses can reuse the common properties and behavior, while also adding their own specific properties and behavior. This allows for code reuse, organization, and flexibility in Java programming.



To summarize, inheritance in Java is a powerful feature that allows classes to inherit the properties and behavior of other classes. Hierarchical inheritance is one type of inheritance where multiple classes inherit from a single superclass. Java supports other types of inheritance such as single inheritance, multiple inheritance (of interfaces), multilevel inheritance, and hybrid inheritance. By understanding and utilizing inheritance effectively, IT students and teachers can write more efficient and organized Java code.


collect
0
avatar
manoj sharma
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more