Inheritance ,Types of Inheritance ,advantages and disadvantages of inheritance in Java
Inheritance in Java:
Definition :-
Inheritance is the process of acquiring the properties by the sub class ( or
derived class or child class) from the super class (or base class or parent
class).
Or
The mechanism of acquiring the properties and functionalities of one class into another class is known as inheritance.
What is child class?
A class that inherits another class is known as child class, it is also known as derived class or subclass.
What is parent class?
The class that is being inherited by other class is known as parent class, super class or base class.
The key word extends is used to define inheritance in Java.
How to use inheritance in Java ?
The keyword used for inheritance is extends.
Syntax of using Inheritance in Java:
syntax of using inheritance in Java is:
class subclass-name extends superclass-name
{
// body of the class
//methods and fields
}
Advantages/ Importance / Benefits of Inheritance :
The main advantages of inheritance are code reusability and readability. When child class inherits the properties and functionality of parent class, we need not to write the same code again in child class. This makes it easier to reuse the code, makes us write the less code and the code becomes much more readable.
1) Software Reusability :
-> Code ( class/package ) can be reused among the projects.
Ex., code to insert a new element into a table can be written once and
reused.
2)Code Sharing ( within a project ) :
-> It occurs when two or more classes inherit from a single parent class.
This code needs to be written only once and will contribute only once to
the size of the resulting program.
3)Increased Reliability (resulting from reuse and sharing of code) :
-> When the same components are used in two or more applications, the
bugs can be discovered more quickly.
4)Information Hiding :
-> The programmer who reuses a software component needs only to
understand the nature of the component and its interface.
It is not necessary for the programmer to have detailed information such
as the techniques used to implement the component.
5) Rapid Prototyping (quickly assemble from pre-existing components) :
-> Software systems can be generated more quickly and easily by
assembling preexisting components.
This type of development is called Rapid Prototyping.
6)Software Components :
-> Inheritance enables programmers to construct reusable components.
7) Polymorphism and Frameworks (high-level reusable components) :
-> Normally, code reuse decreases as one moves up the levels of
abstraction.
Lowest-level routines may be used in several different projects, but
higher-level routines are tied to a particular application.
Polymorphism in programming languages permits the programmer to
generate high-level reusable components that can be tailored to fit
different applications by changes in their low-level parts.
8) Consistency of Interface(among related objects ) :
-> When two or more classes inherit from same superclass, the behavior they
inherit will be the same.
Thus , it is easier to guarantee that interfaces to similar objects are
similar.
Disadvantages of Inheritance :
1)No Independence: One of the main disadvantages of Inheritance in Java is that two classes, both the base and inherited class, get tightly bounded by each other. In simple terms, Programmers can not use these classes independently of each other.
2)Decreases Execution Speed: Another con of Inheritance is that it decreases the execution speed because Inheritance execution takes time and effort.
3)Refactoring the Code: If the user deletes the Super Class, then they have to refactor it if they have used it.
Types of Inheritance :
There are five types of inheritance in Java.
1] Single inheritance
2] Multilevel inheritance
3] Hierarchical inheritance
4] Multiple inheritance
5] Hybrid inheritance
Let us discuss these one by one as follows:
1] Single Inheritance :
Derivation of a class from only one base class is called single inheritance.

Syntax :
class A
{
}
class B extends A
{
}
Or
class BaseClass
{
//methods and fields
}
class DerivedClass extends BaseClass
{
//methods and fields
}
Example :
class Shape{
void draw()
{
System.out.println(“Draw shape”);
}
}
class Circle extends Shape
{
void drwaCircle(){
System.out.println(“Draw Circle”);
}
public static void main(String args[])
{
Circle c = new Circle();
c.draw();
c.drawCircle();
}
}
Output:
Draw shape
Draw Circle
2] Multilevel Inheritance :
The mechanism of acquiring the properties and functionalities of one class into already derived class from another class is known as multilevel inheritance.
Or
A chain of inheritance is called multilevel inheritance.

Synyax :
class A
{
}
class B extends A
{
}
class C extends B
{
}
Or
class grandparent
{
//methods and fields
}
class parent extends grandparent
{
//methods and fields
}
class child extends parent
{
//methods and fields
}
Example :
Write a Java Program to implement multilevel inheritance by applying various access controls to its data members and methods.
Program code:
class students
{
private int sno;
private String sname;
public void getstud(int no,String name)
{
sno = no;
sname = name;
}
public void putstud()
{
System.out.println("Student No : " + sno);
System.out.println("Student Name : " + sname);
}
}
class marks extends students
{
protected int mark1,mark2;
public void getmarks(int m1,int m2)
{
mark1 = m1;
mark2 = m2;
}
public void putmarks()
{
System.out.println("Mark1 : " + mark1);
System.out.println("Mark2 : " + mark2);
}
}
class Result extends marks
{
private int total;
public void puttotal()
{
total = mark1 + mark2;
System.out.println("Total : " + total);
}
public static void main(String args[])
{
Result f = new Result();
f.getstud(100,"omkar dengi");
f.getmarks(90,92);
f.putstud();
f.putmarks();
f.puttotal();
}
}
3] Hierarchical Inheritance :
In hierarchical inheritance, multiple subclasses extend from a single superclass
Or
multiple classes inherits from a single class i.e there is one super class and multiple sub classes.

Syntax :
class X
{
}
class A extends X
{
}
class B extends X
{
}
class C extends X
{
}
Example :
class Laptop
{
void display()
{
System.out.println(“Working...”);
}
class Hp extends Laptop
{
void show()
{
System.out.println("Hp");
}
}
class Dell extends Laptop
{
void print()
{
System.out.println(“Dell Inspiron”);
}
}
class Lenovo extends Laptop
{
void show()
{
System.out.println(“Lenovo YOGA”);
}
}
class TestDemo
{
public static void main(String args[]){
Dell d = new Dell();
d.print();
d.display();
Lenovo l = new Lenovo();
l.show();
l.display();
Hp h=new Hp();
h.show();
h.display();
}
}
Output:
Dell Inspiron
Working...
Lenovo YOGA
Working...
Hp
Working...
4] Multiple Inheritance :
In multiple inheritance, a single subclass extends from multiple superclasses.
Java supports multiple inheritance indirectly through the use of interface. If case of multiple classes, there would be ambiguity. We can overcome this problem with interfaces and defining methods that are needed only.

Syntax :
interface one
{
}
interface two
{
}
class A implements one, two
{
}
Example :
interface Moveable
{
public void run();
}
interface Speakable
{
public void speak();
}
interface Ability extends Moveable, Speakable
{
public void show();
}
class Person implements Ability
{
public void run()
{
System.out.println("I can run !!");
}
public void speak()
{
System.out.println("I can speak !!");
}
public void show()
{
System.out.println("I am a person, I can speak and run !!");
}
}
public class MultipleInheritance
{
public static void main(String[] args)
{
Person obj = new Person();
obj.run();
obj.speak();
obj.show();
}
}
Or
Program to implement the Multiple Inheritance
Program code:
interface Exam
{
void Percent_cal();
}
class Student
{
String name;
int roll_no, Marks1, Marks2;
Student(String n, int rn, int m1, int m2)
{
name = n;
roll_no = rn;
Marks1 = m1;
Marks2 = m2;
}
void show()
{
System.out.println("Student Name :"+name);
System.out.println("Roll no : "+roll_no); System.out.println("Marks1 : "+Marks1); System.out.println("Marks2 : "+Marks2);
}
}
class Result extends Student implements Exam
{
float per;
Result(String n,int rn,int m1,int m2)
{
super(n,rn,m1,m2);
}
public void Percent_cal()
{
int tot = Marks1 + Marks2;
per = (float)tot / 2;
}
void display()
{
show();
System.out.println("Percentage = "+per);
}
}
public class StudentDetails
{
public static void main (String[] args)
{
Result r = new Result("Omkar Dengi",10,75,95);
r.Percent_cal();
r.display();
}
}
Output :
Student Name : Omkar Dengi
Roll No: 10
Marks1: 95
Marks2: 75
Percentage: 85.0
5] Hybrid Inheritance :
A combination of both single and multiple inheritance. Java does not support this type of inheritance.
Hybrid inheritance is a combination of two or more types of inheritance.
Example :
class Ability
{
public void show()
{
System.out.println("I am a person, I can speak and run !!");
}
}
interface Moveable
{
public void run();
}
interface Speakable
{
public void speak();
}
class Person extends Ability implements Moveable, Speakable
{
public void run()
{
System.out.println("I can run !!");
}
public void speak()
{
System.out.println("I can speak !!");
}
}
public class HybridInheritance
{
public static void main(String[] args) {
Person obj = new Person();
obj.run();
obj.speak();
obj.show();
}
}
Why is multiple inheritance and hybrid inheritance not supported by classes in Java?
The basic gist for not supporting multiple inheritance is to avoid ambiguity caused by it. As hybrid inheritance is a mixture of the different types of inheritances that exist. Thus like multiple inheritance, hybrid inheritance also can’t be implemented
Tqsm sir
ReplyDelete