0% found this document useful (0 votes)
39 views18 pages

Chapter 4 Inheritance

Inheritance allows classes to acquire properties of other classes, creating a parent-child relationship where the child inherits from the parent. There are different types of inheritance including single, multilevel, and hierarchical. Method overriding allows subclasses to provide their own implementation of a method in the parent class, while method overloading involves methods with the same name but different parameters within a class.

Uploaded by

Zahida Ali
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)
39 views18 pages

Chapter 4 Inheritance

Inheritance allows classes to acquire properties of other classes, creating a parent-child relationship where the child inherits from the parent. There are different types of inheritance including single, multilevel, and hierarchical. Method overriding allows subclasses to provide their own implementation of a method in the parent class, while method overloading involves methods with the same name but different parameters within a class.

Uploaded by

Zahida Ali
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
You are on page 1/ 18

4.1.

Definition
Inheritance in java is a mechanism in which a class acquires the properties of another class.
The idea behind inheritance in java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields of
parent class, and you can add new methods and fields also.
Inheritance represents the IS-A relationship, also known as parent-child relationship. Here a
class which is extended is known as Base class or Parent class or Super class the class which
is inheriting the properties is known as Derived class or Child class or Sub class.
The subclass can directly access the super class properties. Even it can access protected
members of super class from anywhere.
4.2. Use of inheritance (Why use inheritance in java)
For Code Reusability: The extended class does not have to repeat the existing properties
of its parent.
For changing the existing behaviour: If in case child needs to change the existing
behaviour of its parent then it can do by overriding the methods of its parent. Thus it
achieves run time polymorphism
Syntax:

Extends keyword indicates that you are making a new class that derives from an existing class.
Example: Programmer is the subclass and Employee is the superclass. Relationship between
two classes is Programmer IS-A Employee. It means that Programmer is a type of
Employee. But Employee is not a type of Programmer.
Output:

4.3. Types of inheritance


On the basis of class, there can be three types of inheritance in java: single, multilevel,
hierarchical, multiple (java does not support) and hybrid (java does not support).
(a) Single Inheritance: When a class extends another one class only then we call it a single
inheritance. The below flow diagram shows that class B extends only one class which is
A. Here A is a parent class of B and B would be a child class of A.

B
(a) Single Inheritance
Example:
Output:

(b) Multilevel Inheritance: Multilevel inheritance refers to a mechanism where a class can
inherit a derived class, thereby making this derived class the base class for the new class.
As you can see in below flow diagram C is subclass or child class of B and B is a child
class of A.
A

C
(b) Multilevel inheritance
Example: In this example we have three classes Car, Maruti and Maruti800. We have done
a setup class Maruti extends Car and class Maurit800 extends Maruti. With the help of this
multilevel hierarchy setup our Maurti800 class is able to use the methods of both the classes
(Car and Maruti).
Output:
(c) Hierarchical Inheritance: In such kind of inheritance one class is inherited by many sub
classes. In below example class B, C and D inherits the same class A. A is parent class
(or base class) of B, C & D.
A

B C D
(c) Hierarchical Inheritance
Output:
(d) Multiple Inheritances: The type of inheritance where child class may have multiple
parents but this type of inheritance is not supported by java.
(e) Hybrid inheritance: It is a combination of all types of inheritances but this is also not
supported by java.
Note: Java does not support Multiple Inheritance as if parents have method with same
name and same type signatures then it creates ambiguity when subclass tries to access
that method as it gets confused which method to call.
4.4. Use of super keyword
The subclass can refer its immediate super class by using super keyword.
There are three uses of super keyword:
(a) To refer super class constructor from subclass constructor
Syntax: super(parameters);
It should be first statement in sub class constructor.
Constructors can be referred only from other constructors
(b) To access super class instance variables from subclass
Syntax: super.variable-name
Use: generally it is used when subclass and superclass have same instance variable
name and subclass want to differentiate between those.
(c) To call super class methods from subclass
Syntax: super.method-name(parameters);
Use: generally it is used to call overridden methods of superclass from subclass.
Example:
Output:

Note: Superclass variable can refer subclass object but it cannot access subclass own
properties directly.
Example:

For that we need to again downcast it. Please refer up casting and down casting in the end of
this chapter.
4.5. Method Overriding
If a subclass want to change the original property of superclass. It can use the same method
signature and can add its own implementation. When in superclass as well as subclass, there is
a same method name and type signature then by default, a subclass method overrides the
superclass method. It is runtime polymorphism. Call to an overridden method is decided at run
time and call goes to subclass same method.
Example:
Output:

4.5.1. Rules while overriding the method.


When a subclass is overriding the method of superclass then following factors must be
considered for overriding method in subclass.
(a) Access modifier / Scope: must not be more restrictive than original method i.e. a sub
class cannot reduce visibility of overridden method of superclass
(b) Return type: there are following rules if return type is
Primitive: should be same like original method
Non-primitive: co-variants are allowed. Means if super class' method is returning
any object then subclass' overriding method can return object of its subclass type.
It is added from Java5.
(c) Method name: should be same as overridden method
(d) Type signatures: both the number of parameters and data type of parameters should be
same as original method
(e) Exception handling:
If the superclass method does not declare an exception: If the superclass
method does not declare an exception, subclass overridden method cannot declare
the checked exception but it can declare unchecked exception.
If the superclass method declares an exception: If the superclass method
declares an exception, subclass overridden method can declare same, subclass
exception or no exception but cannot declare parent exception.
Example: 1. Scope

2. Return type : primitive

3. Return type : covariants


4. Exception throwing

4.6. Resolving Method Overriding


If we need to call both the methods, we need to resolve the overriding. There are two ways to
resolve. Resolving a call to an overridden method
(a) Resolving at compile time: We can call the superclass method from sub class method
using super keyword.
Example: Organizations.java
TrainingOrganizations.java
CompileTimeResolvingOverriding.java

(b) Resolving at run time: Dynamic Method Dispatch: Calling the methods dynamically
by changing the object which is referred. We can resolve the call to an overridden method
at runtime by calling the methods dynamically, by referring the objects dynamically.
Hence there is no need of super keyword.
This way is used when
1. We want to achieve Run time polymorphism
2. We cannot change the implementation of subclass methods by adding the statement
of super.
Then the call to above methods from main will be:
4.7. Up casting and down casting
In up casting, children are casted with its parent and it down casting parent is casted with its
children. Super class variable can refer its subclass object directly in up casting, but subclass
variable cannot directly refer its super class object. If a parent is casted with its children, it
throws.
ClassCastException.
Example:

Up casting:

Down casting:

Reason why above down casting is not supported:


Every Training Organization can be an Organization. So we can cast subclass object with
superclass. But every Organization cannot be a Training Organization so we cannot cast the
superclass object with subclass. Thus the down casting given in point 2 is not supported.
4.8. IS-A relationship and HAS-A relationship:
(a) IS-A relationship: This refers to inheritance or implementation. Expressed using

(b) HAS-A relationship:

There is no specific keyword to implement HAS-A relationship but mostly we are


depende
Composition:

own as
composition.
Aggregation:

Aggregation.
Example:

Class Car

IS A

HAS A
Class Maruti Class Engine

Maruti IS A Car. Maruti HAS An Engine.

Thus by inheritance we achieve IS-A relationship and if one object refers another object then
we achieve HAS-A relationship.
4.9. Difference between Method Overloading and Method Overriding

Method Overloading Method Overriding

Same name and same type signatures (both


Same name different type signatures (either
number of parameters and data type should
number of parameters or data type is
be same). here subclass method overrides
different)
superclass method
It can be in same class or in inherited class It is possible only in inheritance

It is compile time polymorphism It is run time polymorphism

Access modifier and return type does not Access modifier and return type matters
matter while overloading the methods as while overriding the method. There are
compiler only checks the parameter certain rules regarding giving scope and
matching return type in sub class

It increases readability of code It increases reusability of code


4.10. Important Points to remember
1. In inheritance, calling sequence of constructors in always from superclass constructor to
subclass constructor.
2. The static methods cannot be overridden as these are class methods and overriding
works on instance at run time. We can have same static method in subclass but the
methods are statically bounded with their class name. Thus calling one method hides
other method. This concept is method over-hiding but not overriding.
3. The private methods cannot be overridden as private methods are not accessible in
subclass. These can be accessed within the superclass by its own object. We can have
same method in subclass but it will be its independent copy.
4. The constructors cannot be overridden as the constructor has same name as classname.

1. Explain? What is Inheritances and Demonstrate How to Extend the class properties?
2. Explain the Different type of Inheritances with Mobile class example.
3. Show the use of super keyword in method and in class with suitable example slike
Vehicle.
4. Write the program for Restrict the class and method from further extend and overriding.
5. Implement the Is-A relationship in between Facebook class and tweeter and whatsup
class.
6. What is the calling sequence of constructors in inheritance?
7. Demonstrate the use of Dynamic Method Disptach in inheritance.
8. Implement the Has-a relationship in between department and trainer.

You might also like