Java is a popular programming language worldwide because of its versatility and compatibility. It finds crucial importance in various fields, including mobile applications, software development and the development of large systems. The mobile operating systems of 88% of the android phones were found to be written in Java in the year 2018. Learning and mastering this programming language opens a broader prospect of employment for the developers.
Concept of Inheritance in Java:
Inheritance is a mechanism in Java in which an object gains all the properties and characteristics of a parent object. Object-oriented programming systems such as Java considers inheritance as one of the important concepts. The key principle behind the Java inheritance concept is that new classes can be built on the basis of the existing classes. The code blocks and methods of the parent class can be reused while inheritance is incorporated from an existing class. The addition of new fields and methods in a newly created class is also possible while a class is inherited. The inheritance in Java denotes the IS-A relationship that is commonly referred to as a parent-child relationship. Inheritance can be implemented in Java with the use of the keyword ‘extends’.
Note: Inheritance is used in Java to implement method overriding and hence ensure the accomplishment of runtime polymorphism. It can also be used to reuse the code.
Check out our free technology courses to get an edge over the competition.
Explore our Popular Software Engineering Courses
Important Terminologies used in Hierarchical Inheritance in Java:
A collection of objects with several properties that are common among them is referred to as a Class. It acts as a blueprint or a template for the creation of objects.
- Sub Class or Child Class:
A class that extends the other class is called a subclass. A subclass is also referred to as a child class or derived class or extended class.
- Super Class or Parent Class:
The class from which the features of the subclass are inherited is called a superclass. It is also called a parent class or a base class.
This is a property of the inherited class by virtue of which the fields and methods of a parent class can be reused in the newly created class. All the methods and fields used in the existing class can be used in the new class with this property.
Hierarchical Inheritance in Java:
Inheritance is a mechanism in which a class or an object is dependent on another class or object to implement the same behavior of the class or object on which it depends. This permits independent extensions of the novel software via public interfaces and classes. It also helps in the mechanism of reusability of the code segments. If a hierarchical relationship exists between the classes or objects, it is referred to as the hierarchical inheritance. A single class serves as a parent class for several derived classes in the hierarchical inheritance.
Hierarchical inheritance in Java is a type of inheritance in which the same class is inherited by more than one class. In other words, when several classes inherit their features from the same class, the type of inheritance is said to be hierarchical. For example, consider 4 classes A, B, C and D, where A is the class from which B, C and D are inherited.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
In-Demand Software Development Skills
Other kinds of Inheritance in Java:
Based on the class, there exist three inheritance types in Java. One of those is already discussed in the previous sections with sufficient example programs. Other than that, Java permits single and multilevel inheritances. In single inheritance, as indicated by the name, one class inherits only one class. i.e., there exists a single subclass for a parent class. A class can inherit from an existing derived class in Multilevel inheritance. i.e., the base class of the newly formed class is a derived class. It is important to note that Java supports multilevel inheritance but not multiple inheritance.
Read our Popular Articles related to Software Development
Limitations of Inheritance in Java:
With the use of inheritance, the parent class and the child class are closely coupled. If the code of the superclass is changed, the subclasses are also affected. So, the parent class and child class are dependent on each other.
If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s Executive PG Programme in Software Development – Specialisation in Full Stack Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.