Professional Documents
Culture Documents
Inheritance Java-2
Inheritance Java-2
INHERITANCE.
Part - 1
BY SAMWEL TARUS
Definition
Derived from the word "Inherit" which means, "to derive / acquire any quality, features or
characteristics from family".
Process where one class acquires the properties (methods and fields) of another class
A mechanism in which one object acquires all the properties and behaviors of a parent object.
A mechanism where the sub or child class inherits the properties and characteristics of the
super class or other derived classes.
When you inherit from an existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
Mechanism allows the new objects to take on the properties of existing objects.
Inheritance builds relationships within the classes; therefore, relating two or more classes to
each other and establishing class hierarchies.
Four pillars of the OOP paradigm are Abstraction, Encapsulation, Inheritance and
Polymorphism.
Benefits of Inheritance
Code reusability:
Minimizing duplicate code:
Key benefits of Inheritance include minimizing the identical code as it allows sharing of
the common code among other subclasses.
Flexibility:
Inheritance makes the code flexible to change, as you will adjust only in one place, and the
rest of the code will work smoothly.
Overriding:
With the help of Inheritance, you can override the methods of the base class.
Data Hiding:
The base class in Inheritance decides which data to be kept private, such that the derived
class will not be able to alter it.
Saves time and effort as the main code need not be written again.
Provides a clear model structure which is easy to understand.
Leads to less development and maintenance costs.
Disadvantages
Decreases the execution speed due to the increased time and effort it takes the
program to jump through all the levels of overloaded classes.
Makes the two classes (base and inherited class) get tightly coupled. This means
one cannot be used independently of each other.
The changes made in the parent class will affect the behavior of child class too.
Refactoring the Code: If the user deletes the Super Class, then they have to
refactor it if they have used it.
Terms used in Inheritance
Class:
Group of objects which have common properties.
Template or blueprint from which objects are created.
Sub Class/Child Class:
A class which inherits the properties of other classes (Base class).
Also called a derived class, extended class, or child class.
Super Class/Parent Class:
Class from where a subclass inherits the features.
Also called a base class or a parent class.
Reusability: Amechanism 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.
Syntax:
//inherits the properties
//of the Father class
class Child extends Father
{
//functionality
}
Points to Remember
Executive.java
class AnimalA{
void eat()
class TestInheritance{
{
System.out.println("eating..."); public static void main(String args[]){
} Dogg d=new Dogg();
}
d.bark();
class Dogg extends AnimalA{
void bark() d.eat();
{ }
System.out.println("barking...");
} }
} TestInheritance.java
Multilevel Inheritance Example
A mechanism in which a single derived class acquires the properties from a single
base class and passes the properties to other single sub-derived classes.
A class is derived from a class which is also derived from another class.
There exists a single base class and single derived class but multiple intermediate
base classes.
The class Marks inherits the
members or methods of the class
Students.
MultilevelInheritance.Java
TestInheritance2.ava
Hierarchical Inheritance
A mechanism in which more than one derived class acquires the properties from a
single base class.
HierarchicalInheritanceExample.java
TestInheritance3.Java
Hybrid Inheritance
• Consist of more than one type of inheritance (combination of two or more types of
inheritance).
• Daughter.java
Multiple Inheritance (Not supported)
MultipleDemo.java
Explanation:
Code gives error because the compiler cannot decide which message() method is
to be invoked.
Due to this reason, Java does not support multiple inheritances at the class level
but can be achieved through an interface.
Super()
Super is a keyword used to refer to the parent class object.
Super keyword came into existence to solve the naming conflicts in inheritance.
When both parent class and child class have members / properties with the same
name, then the super keyword is used to refer to the parent class version.
When both parent class and child class have data members with the same name,
then the super keyword is used to refer to the parent class data member from child
class.
SuperKeywordExample.Java
Super to call parent class constructor
When an object of child class is created, it automatically calls the parent class
default-constructor before it's own.
But, the parameterized constructor of parent class must be called explicitly using
the super keyword inside the child class constructor.
SuperKeywordExample2.Java
Overriding
Subclass / child class has the same method as declared in the parent class.
Subclass provides the specific implementation of the method that has been
declared by one of its parent class
Bike1.Java
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.
OveriddeTest2.Java
Variables defined with the keyword final keyword, becomes a constant, and it
does not allow us to modify the value.
Variable defined with the final keyword allows only a one-time assignment,
FinalVariable1.java
final with methods
The final method extends to the child class, but the child class can not override or
re-define it. It must be used as it has implemented in the parent class.
FinalKeywordExample2.Java
final with class
A class defined with final keyword, cannot be extended by any other class.
FinalKeywordExample3.Java
Assignment