0% found this document useful (0 votes)
3 views13 pages

Understanding Inheritance in Java

Inheritance in Java allows one object to acquire properties from a parent object, promoting code reusability and the ability to redefine methods. It represents an IS-A relationship and includes types such as single, multi-level, and hierarchical inheritance, while multiple inheritance is not directly supported. The document also discusses method overriding, the use of the final keyword, and the concept of blank final variables.

Uploaded by

Vedanshi Aryan
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)
3 views13 pages

Understanding Inheritance in Java

Inheritance in Java allows one object to acquire properties from a parent object, promoting code reusability and the ability to redefine methods. It represents an IS-A relationship and includes types such as single, multi-level, and hierarchical inheritance, while multiple inheritance is not directly supported. The document also discusses method overriding, the use of the final keyword, and the concept of blank final variables.

Uploaded by

Vedanshi Aryan
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

Inheritance in Java

Introduction to Inheritance

▪ It is a mechanism in which one object acquires all the properties of


parent object.
▪ New classes are created based on existing ones.
▪ Can reuse methods and fields of the existing class.
▪ We can add new methods/field to inherited class.
▪ Represents a IS-A (parent-child) relationship.
Few Examples
Super Class

Vehicle Vehicle Animal Animal

Car Bus Dog Cat

Sub class
Use of Inheritance

▪ Code reusability
▪ Re-defining existing methods (Method overriding)
An example code snippet

Class Person{ Class Driver{


int age=20; public static void main(String[] args){
} Person p=new Person();
Faculty f=new Faculty();
Class Faculty extends Person { Students s=new Students();
int courses;
} [Link](“Age= ”+[Link]);
Class Students extends Person{ [Link](“Age= ”+f. courses);
int cgpa; [Link](“Age= ”+[Link]);
} }
Types of Inheritance

Class A Class A
Class A

Class B Class B Class C


Class B
3)Hierarchical

1) Single level
Class C

2) Multi level
Types of Inheritance (Contd…)
▪ Java does not support multiple
inheritance. Class A Class B
▪ It can be supported through
interfaces only.

Class A Class B Class C

Multiple Inheritance

Class C

Class A Class A Hybrid Inheritance


Method Overriding
Class Bank
▪ If a subclass has the same getRate()
method as declared in parent
class, it is called method
overriding.
▪ Subclass provides specific
implementation of the old
method. Class SBI Class ICICI ClassAXIS
getRate() getRate() getRate()
final keyword

▪ It can be used
➢ Variable level -> To stop value change
➢ Method level -> To stop method overriding
➢ Class level -> To stop inheritance
▪ Final methods are inherited but can not be overridden.
Blank final variable

• A final variable that is not initialized at the time of declaration


is called blank final variable.
• It can be initialized using constructor.
class Student{
class Student{
int id;
int id;
String name;
String name;
final String PAN_CARD_NUMBER;
final String PAN_CARD_NUMBER;
Student (id, name, PAN_CARD_NUMBER){
... this. PAN_CARD_NUMBER= PAN_CARD_NUMBER;
} ……
}
...
}
Static Blank Final variable?
Static Blank Final variable?
class A{
• A static and final variable that is not static final int data;
initialized during its declaration. static{ data=50;}
• It is initialized using a static block public static void main(String args[]){
[Link]([Link]);
}
}
final parameter
▪ A final parameter’s value can not be changed.

Class A{
int func(final int n){
n=n+2;
return n;
}
}

Class Driver{
public static void main (String[] args)
{
A a=new A();
[Link](5);
}
}

You might also like