Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Java Inheritance

Uploaded by

Sojal Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Inheritance

Uploaded by

Sojal Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Types of Inheritance

• Single-level inheritance.
• Multi-level Inheritance.
• Hierarchical Inheritance.
• Multiple Inheritance.
• Hybrid Inheritance
• On the basis of class, there can be three types of inheritance in java:

single, multilevel and hierarchical.

• In java programming, multiple and hybrid inheritance is supported

through interface only. We will learn about interfaces later.


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 same method and you call it from child class object,

there will be ambiguity to call method of A or B class.


Inheritance in Java
Inheritance can be defined as the process where one class acquires the
properties (methods and fields) of another class.
class Super
{
.....
.....
}
class Sub extends Super
{
.....
.....
}
class Calculation
public class My_Calculation extends Calculation
{
{
int z; public void multiplication(int x, int y)
public void addition(int x, int y) {
z = x * y;
{
System.out.println("The product of the given
z = x + y; numbers:"+z);
}
System.out.println("The sum of the given
numbers:"+z);
public static void main(String args[])
} {
public void Subtraction(int x, int y) int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
{
demo.addition(a, b);
z = x - y; demo.Subtraction(a, b);
System.out.println("The difference between the demo.multiplication(a, b);
given numbers:"+z);
o/p: The sum of the given numbers:30 }

} The difference between the given numbers:10


}
The product of the given numbers:200
}
Important point
• The Superclass reference
variable can hold the subclass • using the superclass reference variable
object, but using that variable we cannot call the method
we can access only the members multiplication(), which belongs to the
of the superclass. subclass My_Calculation.
• To access the members of both
classes it is recommended to Calculation demo = new My_Calculation();
always create subclass reference demo.addition(a, b); correct

variable. demo.Subtraction(a, b);

demo.multiplication (a, b) ------


My_Calculation demo = new Calculation(); incorrect

// Sub class reference variable cannot hold super class


object
super keyword
• A subclass inherits all the • ‘super’ is used to differentiate
members (fields, methods, and the members of superclass from
nested classes) from its the members of subclass, if they
superclass. have same names.
• Constructors are not members,
so they are not inherited by • It is used to invoke the
subclasses, but the constructor superclass constructor from
of the superclass can be invoked subclass.
from the subclass.
class Super_class public class Sub_class extends
{ Super_class
int num = 20; {
// display method of superclass int num = 10;
public void display() // display method of sub class
{ public void display()
System.out.println("This is the {
display method of superclass"); System.out.println("This is the display
} method of subclass");
} }
public void my_method()
public static void main(String args[])
{
{
// Instantiating subclass
Sub_class sub = new Sub_class();
Sub_class obj = new Sub_class();
// Invoking the display() method of sub class obj.my_method();
sub.display(); }
// Invoking the display() method of superclass }
super.display();
// printing the value of variable num of subclass Output:
System.out.println("value of the variable named
num in sub class:"+ sub.num); This is the display method of subclass
// printing the value of variable num of This is the display method of superclass
superclass value of the variable named num in sub
System.out.println("value of the variable named class:10
num in super class:"+ super.num);
value of the variable named num in super
} class:20
Invoking super class Constructor
• If a class is inheriting the • But if we want to call a
properties of another class, parameterized constructor of the
the subclass automatically superclass, we need to use the super
acquires the default keyword as shown below.
constructor of the superclass.
• super(values);
class Superclass public class Subclass extends Superclass
{ {
int age;
Subclass(int age)
Superclass(int age)
{
{
this.age = age;
super(age);
} }

public void getAge() public static void main(String args[])


{ {
System.out.println("The value of the
variable named age in super class is: " +age);
Subclass s = new Subclass(24);
} s.getAge();
} }
o/p : The value of the variable named age in super class is: 24
}
Imp. Points

• We can only specify one superclass for any subclass that you create.
• Java does not support the inheritance of multiple super classes into a
single subclass. You can create a hierarchy of inheritance in which a
subclass becomes a superclass of another.
• However, no class can be a superclass of itself.
class A class Access {
{ public static void main(String args[]) {
int i; B subOb = new B();
private int j; subOb.setij(10, 12);
void setij(int x, int y) subOb.sum();
{ System.out.println("Total is " + subOb.total);
i = x; }
j = y; }

}
}
REMEMBER A class member that has been
class B extends A
{ declared as private will remain private to its
int total;
void sum()
class. It is not accessible by any code outside
{ its class, including subclasses.
total = i + j; // ERROR, j is not accessible here
Hierarchical Inheritance
• when a class has more than one child classes (sub classes) or in other
words more than one child classes have the same parent class then
this type of inheritance is known as hierarchical inheritance.
class A
class C extends A
{ {
public void methodA() public void methodC()
{ {
System.out.println("method of Class C");
System.out.println("method of Class A");
}
} }
} class JavaExample

class B extends A {
public static void main(String args[])
{
{
public void methodB() B obj1 = new B();
{ C obj2 = new C();
//All classes can access the method of class A
System.out.println("method of Class B");
obj1.methodA();
} obj2.methodA();
} }
}
output

• method of Class A
• method of Class A
Multi level Inheritance
class Shape
{ public void display() {
public class Tester {
System.out.println("Inside display"); public static void main(String[]
} arguments)
}
{
class Rectangle extends Shape {
public void area() Cube cube = new Cube();
{ cube.display();
System.out.println("Inside area");
cube.area();
}
} cube.volume();
class Cube extends Rectangle { }
public void volume()
{
}
System.out.println("Inside volume");
}
}

You might also like