You are on page 1of 29

COM 413 LESSON 7

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.

 The overuse of inheritance makes the program more complex.

 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

 Constructor cannot be inherited in Java.

 Private members do not get inherited in Java.

 Cyclic inheritance is not permitted in Java.

 Assign parent reference to child objects.

 Constructors get executed because of super() present in the constructor.


Types of Inheritance

Java supports the following four types of inheritance:


 Single Inheritance
 Multi-level Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
Single Inheritance / simple inheritance.
 A sub-class is derived from only one super class.

 It inherits the properties and behavior of a single-parent class.

 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.

 A class that has more than one parent 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.

 The class Sports inherits the


members of the class Marks.

 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.

 Uses of super keyword:


To refer parent class data members
To refer parent class methods
To call parent class constructor
Super to refer parent class data members

 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

 Usage of Java Method Overriding:


Used to provide the specific implementation of a method which is already
provided by its superclass / Base class.
Used for runtime polymorphism

 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.

 There must be an IS-A relationship (inheritance).


A real example of Java Method Overriding
 Consider a scenario where Bank is a class that provides functionality to get the
rate of interest. However, the rate of interest varies according to banks. For
example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of
interest.
Pto…
 Java method overriding is mostly used in Runtime Polymorphism

 OveriddeTest2.Java

 Can we override static method?


No, a static method cannot be overridden. It can be proved by runtime
polymorphism
 Why can we not override static method?
It is because the static method is bound with class whereas instance method is
bound with an object. Static belongs to the class area, and an instance belongs to
the heap area.
 Can we override java main method?
No, because the main is a static method.
Difference between method Overloading and Overriding
Method Overloading Method Overriding
 Used to increase the readability of the  Used to provide the specific
program. implementation of the method that is
already provided by its super class.
 Performed within class.  Occurs in two classes that have IS-A
(inheritance) relationship.
 Parameter must be different.  Parameter must be same.

 Example of compile time polymorphism.  Example of run time polymorphism.


 Method overloading can't be performed
by changing return type of the method  Return type must be same or
only. Return type can be same or covariant in method overriding.
different in method overloading. But you
must have to change the parameter.
Final Keyword

 Used with the following things.


With variable (to create constant)
With method (to avoid method overriding)
With class (to avoid inheritance)
final with variables

 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,

 Once a value is assigned to the variable, we cannot change the value.

 FinalVariable1.java
final with methods

 Methods defined with the final keyword, will not allow it to overidde

 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

 Check on Java Constructors in Inheritance.

 Check on Abstract class

 Check on inner classes

You might also like