Hello everyone, in the previous topic we were talking about Java Static Method and Constructor in Java. Today we are going to talk about Inheritance In Java.
Contents
Inheritance in Java
The process by which one class acquires the properties (data members) and functionalities(methods) of another class are called inheritance.
The aim of inheritance is to provide the reusability of code so that a class has to write only the unique features and the rest of the common properties and functionalities can be extended from another class. c) Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Why use inheritance in java
We use inheritance in java –
- For Method Overriding (so runtime polymorphism can be achieved).
- For Code Reusability.
Few Disadvantages of Inheritance
The few disadvantages of Inheritance are as follow-
- The main disadvantage of using inheritance is that the two classes (parent and child class) get tightly coupled.
- This means that if we change the code of parent class, it will affect all the child classes which are inheriting/deriving the parent class, and hence, they cannot be independent of each other.
Terms used in Inheritance
Terms used in Inheritance are as follow-
- Sub Class/Child Class/Base Class- Subclass is a class that inherits the other class. It is also called a derived class, extended class, or child class.
- Super Class/Parent Class/Derived Class- Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
The syntax of Java Inheritance class Subclass-name extends Superclass-name { //methods and fields } |
The “extends” keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.
Java Inheritance Example class Parent { public void System.out.println(“Parent method”); { public class Child extends Parent { Public void c 1() { System.out.printin(“Child method”); } public static void main(String[] args) { ( Child cobj = new Child(); cobj.c10: //method of Child class cobjp10: //method of Parent class } } Output- Child method Parent method |
Another Example class Super_Class { String sup; } public class Sub_Class extends Super_Class{ String sub; public void show detail () { Sup=” JAVA” sub=”Programming” System.out.printin(sup+””+sub); } public static void main(String[] args) { Sub Class obj = new Sub_Class(); obj showDetail(); } } Output- JAVA Programming |
Different types of inheritance are supported by Java
Following are the different types of inheritance are supported by Java-
- Single Inheritance
- Multiple Inheritance (Through Interface)
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance (Through interface)
Note: Multiple inheritance and hybrid is not supported in Java through the class.
Single Inheritance In Java
When a class extends another class(Only one class) then we call it Single inheritance. The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Subclass and Class A will be the one and only Superclass.
Example of Single Inheritance class Shape { public void display() { System.out.println(“Inside display”); } } class Rectangle extends Shape { public void area() { System.out.println(“Inside area”); } } public class Tester { public static void main(String[] arguments) { Rectangle rect = new Rectangle(); rect.display(); rect.area(); } } Output Inside display Inside area |
Multilevel Inheritance in Java
In Multilevel Inheritance, a derived class will be inheriting a parent class, and as well as the derived class act as the parent class to another class.
Example of Multilevel Inheritance class Animal{ void eat(){System.out.println(“eating…”);} } class Dog extends Animal{ void bark(){System.out.println(“barking…”);} } class BabyDog extends Dog{ void weep(){System.out.println(“weeping…”);} } class TestInheritance2{ public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }} Output- weeping… barking… eating… |
Hierarchical Inheritance in Java
In Hierarchical inheritance, one parent class will be inherited by many sub-classes.
As per the below example, ClassA will be inherited by ClassB, ClassC and ClassD ClassA will be acting as a parent class for ClassB, ClassC, and ClassD.
Example of Hierarchical Inheritance class Animal{ void eat(){System.out.println(“eating…”);} } class Dog extends Animal{ void bark(){System.out.println(“barking…”);} } class Cat extends Animal{ void meow(){System.out.println(“meowing…”);} } class TestInheritance3{ public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error }} Output- meowing… eating… |
Hybrid Inheritance
Hybrid inheritance in Java is a combination of two or more types of inheritances. The purpose of using hybrid inheritance in Java is to modularize the codebase into well-defined classes and provide code reusability.
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritances are not supported in java Consider a scenario where A. B and C are three classes. The C class inherits the 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.
Aggregation in Java
If a class has an entity reference, it is known as Aggregation. Aggregation represents the HAS-A relationship.
For eg- consider a situation, Employee object contains many informations such as id, name, email I’d, etc. It contains one more object named address, which contains its own information such as city, state, country, zip code, etc.
When we should use Aggregation?
Code reuse is also best achieved by aggregation when there is no relationship. Inheritance should be used only if the relationship is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.
Example Of Aggregation Class Operation{ int square(int n){ return n*n; } } class Circle{ Operation op;//aggregation double pi=3.14; double area(int radius){ op=new Operation(); int rsquare=op.square(radius);//code reusability (i.e. delegates the method call). return pi*rsquare; } public static void main(String args[]){ Circle c=new Circle(); double result=c.area(5); System.out.println(result); } } Output 78.5 |
So, that is all for today guys see you in our next blog. If you like our article please don’t forget to share with others & follow our Instagram page for your daily dose of Motivation.
Thank You,
Regards
Grooming Urban
General FAQ
What is Inheritance in Java?
The process by which one class acquires the properties (data members) and functionalities(methods) of another class are called inheritance.
Why use inheritance in java?
We use inheritance in java –
a. For Method Overriding (so runtime polymorphism can be achieved).
b. For Code Reusability.
What are the few disadvantages of Inheritance?
The few disadvantages of Inheritance are as follow-
a. The main disadvantage of using inheritance is that the two classes (parent and child class) get tightly coupled.
b. This means that if we change the code of parent class, it will affect all the child classes which are inheriting/deriving the parent class, and hence, they cannot be independent of each other.
What are the terms used in Inheritance?
Terms used in Inheritance are as follow-
1. Sub Class/Child Class/Base Class- Subclass is a class that inherits the other class. It is also called a derived class, extended class, or child class.
2. Super Class/Parent Class/Derived Class- Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
What are the different types of inheritance are supported by Java?
Following are the different types of inheritance are supported by Java-
a. Single Inheritance
b. Multiple Inheritance (Through Interface)
c. Multilevel Inheritance
d. Hierarchical Inheritance
e. Hybrid Inheritance (Through interface)
What is single inheritance In Java?
When a class extends another class(Only one class) then we call it Single inheritance.
What is multilevel inheritance in Java?
In Multilevel Inheritance, a derived class will be inheriting a parent class, and as well as the derived class act as the parent class to another class.
What is hierarchical inheritance in Java?
In Hierarchical inheritance, one parent class will be inherited by many sub-classes.
What is hybrid inheritance?
Hybrid inheritance in Java is a combination of two or more types of inheritances. The purpose of using hybrid inheritance in Java is to modularize the codebase into well-defined classes and provide code reusability.
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritances are not supported in java Consider a scenario where A. B and C are three classes. The C class inherits the 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.
What is aggregation in Java?
If a class has an entity reference, it is known as Aggregation. Aggregation represents the HAS-A relationship.
When we should use Aggregation?
Code reuse is also best achieved by aggregation when there is no relationship. Inheritance should be used only if the relationship is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.
1 thought on “Inheritance In Java and types of inheritance supported by Java”