Java Inheritance
Java Inheritance
Java
Inheritance
Rita Nagar
Assistant Professor, CSE Department MPGI
Outlines
• Prerequisite
• Inheritance
• How can we implement inheritance?
• Why do we need inheritance?
• Types of inheritance
• References ,
Prerequisite
• Class
• Object creation
Java Inheritance
In Java, A class that inherits from another class
can reuse the methods and fields of that class. In
addition, you can add new fields and methods to
your current class as well.
Class (parent,base,super)
•Extensibility: The inheritance helps to extend the functionalities of a class. If you have
a base class with some functionalities, you can extend them by using the inheritance in
the derived class.
•Abstraction: The concept of abstract where we do not have to provide all details is
achieved through inheritance. Abstraction only shows the functionality to the user.
Student
University
Student
Courses
University
C
Hierarchical Inheritance
B A
Single Inheritance
D
Method overriding
• Declaring a method in sub class which is already present in parent class is known
as method overriding.
• The main advantage of method overriding is that the class can give its own
specific implementation to a inherited method without even modifying the
parent class code.
• This is helpful when a class has several child classes, so if a child class needs to
use the parent class method, it can use it and the other classes that want to
have different implementation can use overriding feature to make changes
without touching the parent class code.
• Method overriding is used to provide the specific implementation of a method
which is already provided by its superclass.
• Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
• There must be an IS-A relationship (inheritance).
Is-A relationship: Whenever one class inherits another class, it is called an IS-A relationship
Example
polymorphism
• Polymorphism is one of the OOPs feature that allows us to perform a single action
in different ways.
In case of method overloading, parameter must In case of method overriding, parameter must
3)
be different. be same.
Method overloading is the example of compile Method overriding is the example of run time
4)
time polymorphism. polymorphism.
In java, method overloading can't be performed
by changing return type of the method only.
Return type must be same in method
5) Return type can be same or different in method
overriding.
overloading. But you must have to change the
parameter.
Final Variable
• A final variable, also called a “constant,” is a variable whose value you can't change after it's
been initialized.
• For example, you might use a final variable to define a constant value, such as pi.
When a variable is declared with the final keyword, its value can’t be changed, essentially, a constant. This
also means that you must initialize a final variable.
References
1) https://www.geeksforgeeks.org/inheritance-in-java/
2) https://www.javatpoint.com/inheritance-in-java
3) https://www.w3schools.com/java/java_inheritance.asp
4) E Balagurusamy Programming with JAVA McGraw Hill Publications
5) Naughton & Schildt The Complete Reference Java 2 Tata McGraw Hill
6) Horstmann & Cornell “Core Java 2” (Vol I & II ) Sun Microsystems
7) Deitel Java- How to Program Pearson Education, Asia
8) lvan Bayross Java 2.0 BPB publications
21