0% found this document useful (0 votes)
2 views

Inheritance in Java

Inheritance in Java allows classes to inherit properties and methods from other classes, promoting code reusability and method overriding. There are three main types of inheritance: single, multilevel, and hierarchical, each illustrated with examples. Java does not support multiple inheritance to avoid ambiguity, but it can be achieved using interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Inheritance in Java

Inheritance in Java allows classes to inherit properties and methods from other classes, promoting code reusability and method overriding. There are three main types of inheritance: single, multilevel, and hierarchical, each illustrated with examples. Java does not support multiple inheritance to avoid ambiguity, but it can be achieved using interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

*: Inheritance in Java :*

Inheritance is a mechanism in which the properties and methods are


inherited by one or more classes.

Why use inheritance in java :-

o For Method Overriding (so runtime polymorphism can be achieved).


o For code Reusability (reusability is a mechanism which facilitates you to reuse the fields
and methods of the existing class when you create a new class. You can use the same
fields and methods already defined in the previous class.)
Types of Inheritance :-

➢ Single Inheritance with Example :- When a class inherits another class, it is known as
a single inheritance.
Example :–

class Parent {
void disp() {
System.out.println("Hello, I am Srimanta Maity,father of Subhankar");
}
}
class Child1 extends Parent {
void show() {
System.out.println("Hi, I am Subhankar.");
}
}
public class singleInheritance {
public static void main(String[] args) {
Child1 c = new Child1();
c.disp();
c.show();
}
}

Output –
Hello, I am Srimanta Maity,father of Subhankar

Hi, I am Subhankar.

➢ Multilevel Inheritance with Example :- When there is a chain of inheritance, it is


known as multilevel inheritance.

Example :-

class Parent {
void disp() {
System.out.println("Hello, I am Srimanta Maity,father of Subhankar");
}
}
class Son extends Parent {
void show() {
System.out.println("Hi, I am Subhankar.");
}
}
class Child extends Son {
void view() {
System.out.println("Hi, I am a child.");
}
}
public class multilevelInheritance {
public static void main(String[] args) {
Child c = new Child();
c.view();
c.show();
c.disp();
}
}
Output: -
Hi, I am a child.

Hi, I am Subhankar.

Hello, I am Srimanta Maity,father of Subhankar


➢ Hierarchical Inheritance with Example :- When two or more classes inherits a single
class, it is known as hierarchical inheritance.

Example –

class Parent {

void disp1() {
System.out.println("Hello, I am Srimanta Maity,father of Bubu & Debu");
}
}
class Son1 extends Parent {
void show() {
System.out.println("Hi, I am Bubu.");
}
}
class son2 extends Parent {
void show() {
System.out.println("Hi, I am Debu.");
}
}
public class hierarchicalInherit {
public static void main(String[] args) {
Son1 s1 = new Son1();
son2 s2 = new son2();
s2.disp1();
s2.show();
s1.disp1();
s1.show();
}
}
Output –
Hello, I am Srimanta Maity,father of Bubu & Debu

Hi, I am Debu.

Hello, I am Srimanta Maity,father of Bubu & Debu

Hi, I am Bubu.
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not
supported in java. Consider a scenario where A, B, and C are three classes. The C class
inherits 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. Since
compile-time errors are better than runtime errors, Java renders compile-time error if
you inherit 2 classes. So whether you have same method or different, there will be
compile time error.

• This confusion is reduce by using multiple interfaces to achieve multiple


inheritance.

Example :–

interface Father {
double ht = 5.6;
}
interface Mother {
double ht = 5.0;
}
class Son4 implements Father, Mother {
double ht;
void calculate() {
ht = (Father.ht + Mother.ht) / 2;
System.out.println("Son height is average of father &
mother=" + ht);
}
}
public class multipleInheritance {
public static void main(String[] args) {
Son4 s = new Son4();
s.calculate();
}
}

Output:
Son height is average of father & mother=5.3

You might also like