Inheritance in Java
In this post, we’re going to talk about inheritance in Java. In a previous life, I was an evolutionary biologist working on modelling microbial populations. As you might imagine, it’s not easy to get started without knowing what you’re doing. There are so many different factors that go into creating a living thing — its DNA sequence, its habitat, its biology and so on — that we have to think in broad strokes when trying to understand the details of our world. Inheritance is one of the most basic ways for organisms to share certain characteristics across generations. It also happens to be one of the oldest and most effective ways for software developers to leverage existing code bases and share features between projects. You can learn a lot by starting with an example: after all, imitation is the highest form of flattery. So let’s take a look at what inheritance is, what it does, and how it can be used in your day-to-day development practices.
What is inheritance in Java?
Inheritance is the practice of inheriting characteristics, properties, or functionality from one parent program to another. Every now and then, you’ll see people refer to it as “automating inheritance,” since it’s often implemented by computer programs as “subclassing.” Inheritance is a tool for sharing features and characteristics between related classes. It leverages existing code bases and can be used to share data types, methods, and other features between projects.
Why use inheritance?
automating inheritance Let’s say you’re developing a grocery checkout app. You might have a shopping cart app that allows customers to add items to their cart, a checkout process that allows customers to pay for items they’ve selected, and a portal that customers can use to check out and pay for their items. All of these could be separate projects, but they could all be developed together in one codebase. Using inheritance, you can add common functionality among your apps, such as adding an item to a shopping cart, creating a checkout process for various payment methods, and a customer portal that includes a shopping cart and payment page.
How does inheritance work in Java?
The basics of inheritance in Java are as follows: Every class in a project inherits from a superclass. For example, the Shopping Cart App Superclass inherits from the Me class. the class inherits from the class. A subtype of a supertype is appended to the class. For example, the Me subclass is appended to the Shopping Cart Superclass. the subclass is appended to the class. In some cases, a base class is added to the class. For example, the Object subclass is appended to the Me class. the subclass is appended to the class. Finally, any methods, variables, or functions within the class are inherited from the superclass.
Types of inheritance in java
We will look at each type of inheritance in Java below. In the examples and flow diagrams, we will verify which type of inheritance is present.
Single Inheritance: An easy way to understand single inheritance is to think of it as a single parent. A class that extends another one is known as a single inheritance when it only does so after a certain stage in the inheritance chain. In the flow diagram, B extends only one class, A. A is a parent of B, and B is a child of A.
An example Java programme dealing with single inheritance.
Class X
{
public void methodX()
{
System.out.println("Hello");
}
}
Class X extends Y
{
public void methodY()
{
System.out.println("World");
}
public static void main(String args[])
{
Z obj = new Y();
obj.methodX(); //calling super class method
obj.methodY(); //calling local method
}
}
You can run this code here and compile your own code on Interviewbit.
Multiple Inheritance: Multiple inheritances is the phenomenon in which a class extends multiple classes, thus creating a single class that extends multiple classes. For example, Class C extends classes A and B, resulting in multiple inheritances. Java does not recognize this kind of inheritance. We will discuss how we can employ interfaces instead of classes in the same vein in this article.
Why Java doesn’t support multiple inheritances?
Java does not permit multiple inheritances, thus avoiding the ambiguity that results from it. C++, Common Lisp , and a few other languages may support multiple inheritances. The diamond issue happens in multiple inheritances, because it is unclear whether or not it exists.
What is the diamond problem?
We can see the diagram below to assist us in processing multiple inheritances. Class D extends both classes B and C, yet both classes B and C override that method. This is a real issue. D, since it extends both B and C, must choose which method to use, even if that method is the same in both classes. Because D is extending both B and C if D wants to use the same method, which method will be invoked (the overridden method of B or the overridden method of C. The problem is that Java does not support multiple inheritances.
Multilevel Inheritance: An inheritance mechanism in OO technology that makes a derived class a base class for its child classes is called multilevel inheritance. In the flow diagram below, C is a subclass of B and B is a child of A. A derived class inherits from B, thereby turning it into the base class for its child classes.
Multilevel inheritance example programme in Java:
Class A
{
public void methodA()
{
System.out.println("Hello");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("World");
}
}
Class C extends B
{
public void methodC()
{
System.out.println("Coders");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA(); //calling grand parent class method
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}
You can run this code here.
Hierarchical Inheritance: The same class is inherited by many subclasses in such a sequence of generations. In the preceding example, classes B,C, and D all inherited class A, making A their parent class (or base class).
Code:
Class X {
// fields and methods
}
class X1 extends X {
// fields and methods
}
class X2 extends X {
// fields and methods
}
class X3 extends X {
// fields and methods
}
You can run this code here.
Hybrid Inheritance: Hybrid inheritance, in its simplest form, involves combining single and multiple inheritances. A typical flow chart, for example, would resemble the one below. In Java, hybrid inheritance may be achieved in a similar way to multiple inheritances. Yes, you've got it right. Using interfaces is how you can do it.
public class X
{
// Methods and Fields of class X
}
public interface X1
{
// Methods and Fields of X1
}
public interface X2
{
// Methods and Fields of X2
}
public class Z extends X implements X1,X2
{
// Implementation of the method defined in Interfaces, X1 and X2
// Methods of class Z
}
You can run this code here.
Conclusion
Inheritance is a crucial concept in programming that allows us to reuse code and re-use code bases. It’s also an essential building block for every object-oriented language, including Java. Inheritance allows us to share features and functionality between related classes. It’s also responsible for creating a genetic code that produces biological phenomena, such as how we inherit characteristics from our parents and the ability to speak. And with inheritance being such an important concept in programming, it’s important to understand how it works and how it’s used. In this post, we’ve gone over what inheritance is, what it does, and how it can be used in your day-to-day development practices.
Discussion