Types of inheritAnce in java (oop)object oriented programming
Computer programs are modeled using the object-oriented programming (OOP) paradigm as a set of interconnected objects. OOP is distinct from other approaches to programming in that it implements reusable code in the form of classes.
a laptop user using Java's inheritance featureinheritence
OOP-style programs use a mechanism called inheritance to enable this reusability. The primary goal of inheritance is to allow programmers to create new classes—also referred to as "child classes" or "children"—that inherit the attributes and methods of older classes (also referred to as "parent classes") without having to write new code.
A simple and effective tool for simulating the real world is inheritance. Consider the idea that children inherit certain physical characteristics and behavioral traits from their parents. Many of these real-world phenomena can be modelled particularly well using inheritance.
Consider the possibility that you want to create a game about penguins. You'll need a parent class called penguin for this, as it defines the attributes and methods that all of its child classes must have. Then, in addition to the attributes and methods already obtained from the more generic parent class, you can add more special attributes and methods to these child classes. As a result, there is no longer a need to copy and paste code into each child class from the parent class.
Because of this, many programming languages—including Java, the subject of this article—implement OOP. Java helped spread the use of object-oriented programming (OOP) in the 1980s by making it easier to implement the four fundamental OOP principles of inheritance, encapsulation, abstraction, and polymorphism.
You'll discover the five different types of Java inheritance in this article and gain a better understanding of how it functions when representing real-world objects in code.
What does Java's inheritance mean?
In general, inheritance happens when one or more parents, or super classes, extend their functionalities and attributes to one or more children, or subclasses. There are five different inheritance structures available in Java: single, multilevel, hierarchical, multiple, and hybrid.
Java's Inheritance Types
Now let's examine the various types of inheritance and how they manifest themselves in Java programs.
Individual Inheritance/Single
The Java language uses the keyword extends to signify that a class is inheriting properties from another. Because of inheritance, the grand Parent Main method of the Animal class will be present in the Penguin class without needing to be rewritten there.
Consider the general illustration below to better understand single inheritance, in which the child class derives from a single base class:
Java
public class Animal {
protected String featherColor = "White";
public void setFeatherColor(String newColor) {
this.featherColor = newColor;
}
public void grandParentMain() {
System.out.println("This is the base class. It represents any animal.");
}
}
public class Penguin extends Animal {
String featherColor = "Black";
public void parentMain() {
System.out.println("The parentMain method returns a Penguin with " + featherColor + " feathers.");
}
}
You can easily test this idea by creating a simple program that instantiates a subclass and calls both methods (click the “output” tab to view the output of the code).
Javaoutput
public class mainTest{
public static void main(String[] args) {
Penguin penguin = new Penguin();
penguin.grandParentMain();
penguin.parentMain();
}
}
Using the code above, you can confirm the child class’s ability to use its parent’s methods.
Multilevel Inheritance
Multilevel inheritance occurs when one class inherits from a second class that already inherits from a third class.
To illustrate this concept, consider the two classes introduced in the previous section. You’ll now add a third class to describe a species within the Penguin class. This class will be called Emperor to represent an Emperor penguin. Because it inherits from Penguin, the Emperor class has access to all the attributes and methods of both the Penguin child class and the Animal base class.
Java
public class Emperor extends Penguin {
int weight = 30; // kg
int height = 115; // cm
public void childMain() {
System.out.println("A special kind of penguin is called the Emperor penguin. It weighs about " + weight + " kg and measures around " + height + " cm tall.");
}
}
Adding this specificity requires adding new levels to the “object tree.” As the name suggests, this is multilevel inheritance.
The following code sample tests this concept by instantiating an Emperor object and demonstrating that it can access all three methods:
Javaoutput
class mainTest{
public static void main(String[] args) {
Emperor emperor = new Emperor();
emperor.grandParentMain();
emperor.parentMain();
emperor.childMain();
}
}
Hierarchical Inheritance
Suppose that you now want to add more species of penguins to your game to accompany the Emperor penguins. You’ll create a new class for Gentoo penguins, who are distinguished primarily by their orange beaks:
Javaoutput
public class Gentoo extends Penguin {
String beakColor = "Orange";
public void dance() {
System.out.println("I have a(n) " + beakColor + " beak and love to dance.");
}
}
class mainTest{
public static void main(String[] args) {
Gentoo gentoo = new Gentoo();
gentoo.grandParentMain();
gentoo.parentMain();
gentoo.dance();
}
}
As you can see, the Gentoo class inherits from Emperor's methods while also including a few customized traits specific to Gentoo penguins. Additionally, attempting to access the Emperor class' child Main method will result in an error because it does not inherit from the Emperor class. Architecture hierarchical inheritance refers to this.
Several Inheritances
When a single child class inherits from two parent classes, this is known as multiple inheritance. This type of inheritance frequently causes a lot of hassle and leads to challenging issues. For instance, if two methods have the same ID signature, the compiler is unable to distinguish or prioritize between them. Java does not support this kind of multiple inheritance for this reason.
Java 8 however provided a different method to implement multiple inheritance, utilizing Java interfaces and
Javaoutput
-Escaped HTML-
interface Penguin1 {
default void show() {
System.out.println("I am Penguin1.");
}
}
interface Penguin2 {
default void show() {
System.out.println("I am Penguin2.");
}
}
class TestClass implements Penguin1, Penguin2 {
// Overriding default show method
public void show()
{
// Using super keyword for P1
Penguin1.super.show();
// Using super keyword for P2
Penguin2.super.show();
}
public static void main(String args[])
{
TestClass test = new TestClass();
test.show();
}
}
The aforementioned illustration represents each parent using an interface rather than a class. The method show has the same identifier for both parents. Both of the parents are inherited by the TestClass class.
You implement a third show method in the child class to override both methods in order to achieve multiple inheritance. You then specify which parent's method you want to call by using the keyword super. As a result, you call show from Penguin1 first, and Penguin2 second.
hybrid succession
Developers frequently combine two inheritance techniques to increase their flexibility. Hybrid inheritance is the term for this practice. Hybrid inheritance aims to modularize the codebase and create classes with clear definitions.
Suppose you introduce a new class:
Java
public class Catbird extends Animal{
public void show(){
System.out.println("I am a Catbird with " + featherColor + " feathers.");
}
}
You’ll then change the method names in your previous classes to show. You now have a hierarchical inheritance since both Penguin and Catbird inherit from Animal. You also have a multilevel inheritance because Gentoo inherits from Penguin.
This example also demonstrates the usage of protected members. Looking back at the Animal class, you used the keyword protect to restrict the accessibility of the featherColor attribute. This keyword makes it available only to methods within the class or those that inherit from it.
Javaoutput
class mainTest{
public static void main(String[] args) {
Catbird catbird = new Catbird();
catbird.show();
catbird.setFeatherColor("Gray");
catbird.show();
}
}
Comparing Polymorphism and Inheritance
Because inheritance makes code reuse easier, you can make various instances of a parent class without having to rewrite any code. It's simple to mix up polymorphism and inheritance because they both refer to similar concepts. They aren't the same, though.
The distinction is that polymorphism permits individualization. By allowing child classes to make minor changes to the inherited methods, polymorphism enables you to choose which method to compile based on the input parameters. For more information, see our article on polymorphism in Java.
Using Java Inheritance Inheritance is a fantastic tool to increase code reuse and model the real world, and Java provides you with the necessary tools to easily incorporate inheritance into your toolkit. The adaptability offered by its five types
Comments
Post a Comment