0% found this document useful (0 votes)
2 views3 pages

Java Inheritance Types Explained

The document explains the concept of inheritance in Java, detailing its purpose, types, and examples. It covers single, multi-level, multiple, and hierarchical inheritance, highlighting how subclasses inherit properties from superclasses. Additionally, it notes that Java does not support multiple inheritance through classes but can utilize interfaces to achieve similar functionality.

Uploaded by

dahalrochak19
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 views3 pages

Java Inheritance Types Explained

The document explains the concept of inheritance in Java, detailing its purpose, types, and examples. It covers single, multi-level, multiple, and hierarchical inheritance, highlighting how subclasses inherit properties from superclasses. Additionally, it notes that Java does not support multiple inheritance through classes but can utilize interfaces to achieve similar functionality.

Uploaded by

dahalrochak19
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

Unit 4 Inheritance & Packaging

Inheritance:
Inheritance is the process by which object of one class acquire to inherit the properties of an object of
another class.
“A derived class which inheritance the properties of his base class is classed inheritance”

What is the use of inheritance and why it is necessary?


o We can reuse code from the base classes
o In java extends keyword is used to perform inheritance.
o We can’t access private members of class through inheritance.
o A sub class contains all the features of Super class so we should create the object of sub class.
o Method overriding only possible through inheritance.

Type of inheritance:
1. Single inheritance
2. Multi-level inheritance
3. Multiple inheritance
4. Hierarchical inheritance

Single inheritance:
Single inheritance is nothing but which contain only one Super class and only one Sub class is called
single inheritance.
Example:
Super class
class SuperClass
{
int emp_id;
String emp_name;
} Sub class
public class SingleInheritance extends SuperClass
{
void displayMethod()
{
emp_id = 101;
emp_name = "Abhiraj";
[Link]("Emp id : " + emp_id + "\n" + "Emp Name: "+ emp_name);
}
Output:
public static void main(String[] args) {
SingleInheritance obj = new SingleInheritance();
[Link]();
}
}
Multi-level inheritance:

In multi-level inheritance we have only one super class and multiple sub classes are called multi-level
inheritance.
Supler Class
Example:
class SuperClasss
{
Sub1 Class
int num1, num2, result;
void sum()
{
num1 = 10;
num2 = 20; Sub2 Class
result = num1 + num2;
[Link]("Sum of tow numbers : " + result);
}
void sub()
{
num1 = 30;
num2 = 20;
result = num1 - num2;
[Link]("Sub of tow numbers : " + result);
}
}
class SubClass1 extends SuperClasss
{
void prod()
{
num1 = 5;
num2 = 10;
result = num1 * num2;
[Link]("Prod of tow numbers : " + result);
}
}
class SubClass2 extends SubClass1
{
void div() {
num1 = 10;
num2 = 5;
result = num1 / num2;
[Link]("Div of tow numbers : " + result);
}
} Output:
public class MultilevelInheritance {

public static void main(String[] args) {


SubClass2 obj = new SubClass2();
[Link]();
[Link]();
[Link]();
[Link]();
}

}
Multiple inheritance:
Java doesn’t support multiple inheritance but we can overcome this problem using interface. Because a sub
class wants to inherit the property of two or more Super classes that have same method. Java compiler can’t
decide which class method it should inherit i.e. a reason java doesn’t support multiple inheritance through
classes.

Hierarchical inheritance:
A hierarchical inheritance which contain only one Super class and multiple Sub class and all Sub class
directly extend Super class is called hierarchical inheritance.
Example:

class SuperClassss { Super Class


void input() {
[Link]("Your name.");
}
} Sub Class1 Sub Class2 Sub Class1
class SubClass11 extends SuperClassss {
void show() {
[Link]("My name is Abhiraj.");
}
}
class SubClass22 extends SuperClassss {
void display() {
[Link]("My name is Ananya.");
}
}
class SubClass33 extends SuperClassss {
void display() {
[Link]("My name is Nawaraj.");
}
}
public class HierarchicalInheritance { Output:
public static void main(String[] args) {
SubClass11 obj1=new SubClass11();
SubClass22 obj2 = new SubClass22();
SubClass33 obj3=new SubClass33();

[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}

You might also like