4-Lecture-four new

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Chapter 04

INHERITANCE and POLYMORPHISM


Inheritance
• In Java programming, the inheritance is an important
concept of Java OOPs.
• Inheritance is a process where one class acquires the
properties (methods and attributes) of another. With the
use of inheritance, the information is made manageable
in a hierarchical order.
• The class which inherits the properties of other is known
as sub class (derived class, child class) and the
class whose properties are inherited is known as super
class (base class, parent class).
Why do we need Inheritance?
• Code Reusability: The basic need of an inheritance is to reuse
the features. If you have defined some functionality once, by
using the inheritance you can easily use them in other classes
and packages.
• 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.
• Implementation of Method Overriding: Inheritance is required
to achieve one of the concepts of Polymorphism which is Method
overriding.
• Achieving Abstraction: Another concept of OOPs that is
abstraction also needs inheritance.
Implementation of Inheritance in
Java
• The extends keyword is used for the implementation of
inheritance in Java. It inherits the properties (attributes
or/and methods) of the base class to the derived class.
The word "extends" means to extend functionalities i.e.,
the extensibility of the features.
• Syntax:
Java Inheritance: Example
Java Inheritance: Example
• Previous example demonstrates Java inheritance.
• In this example, you can observe two classes namely
Calculation and My_Calculation.
• Using extends keyword, the My_Calculation inherits the
methods addition() and Subtraction() of Calculation
class.
Java Inheritance: Example…
• In the given program, when an object
to My_Calculation class is created, a copy of the
contents of the superclass is made within it.
• That is why, using the object of the subclass you can
access the members of a superclass.
Inheritance: Example…
• The Superclass reference variable can hold the subclass object,
but using that variable you can access only the members of the
superclass, so to access the members of both classes it is
recommended to always create reference variable to the
subclass.
• If you consider the above program, you can instantiate the class
as given below.
• But using the superclass reference variable ( demo in this case)
you cannot call the method multiplication(), which belongs to
the subclass My_Calculation.
The this keyword in Java
• Let’s begin with an example:
The this keyword in Java…
• The this keyword refers to the current object in a
method or constructor
• The most common use of the this keyword is to
eliminate the confusion between class attributes and
parameters with the same name (because a class
attribute is shadowed by a method or constructor
parameter).
• If you omit the keyword in the example above, the
output would be "0" instead of "5".
The this keyword in Java…
• this can also be used to:
• Invoke current class constructor
• Invoke current class method
• Return the current class object
• Pass an argument in the method call
• Pass an argument in the constructor call
Constructors in Inheritance
• A subclass inherits all the members (fields, methods,
and nested classes) from its superclass.
• Constructors are not members, so they are not inherited
by subclasses, but the constructor of the superclass can
be invoked from the subclass.
• We use the super keyword to do this
Java Inheritance: the super keyword
• The super keyword is similar to this keyword.
• The super() is mainly used for invoking base class
member functions and constructors.
• Following are the scenarios where the super keyword is
used:
• It is used to differentiate the members of superclass from
the members of subclass, if they have same names.
• It is used to invoke the superclass constructor from
subclass. It always comes in the first line of the child class
constructor
Inheritance: the super keyword…
• Differentiating the members
• If a class is inheriting the properties of another class, and if
the members of the superclass have the names same as the
sub class, we use super keyword as shown below to
differentiate these variables:
Inheritance: the super keyword…
• Example:
Inheritance: the super keyword…
The super keyword: example
• In the given program, you have two classes
namely Sub_class and Super_class, both have a method
named display() with different implementations, and a
variable named num with different values.
• We are invoking display() method of both classes and
printing the value of the variable num of both classes.
Here you can observe that we have used super keyword
to differentiate the members of superclass from
subclass.
Invoking superclass constructor
• If a class is inheriting the properties of another class, the
subclass automatically acquires the default constructor of
the superclass.
• But if you want to call a parameterized constructor of the
superclass, you need to use the super keyword as shown
below.

• Look at the next sample code:


Invoking superclass constructor…
IS-A Relationship
• IS-A is a way of saying: This object is a type of that
object. Let us see how the extends keyword is used to
achieve inheritance.
IS-A Relationship …
• Now, based on the above example, in Object-Oriented
terms, the following are true:
• Animal is the superclass of Mammal class.
• Animal is the superclass of Reptile class.
• Mammal and Reptile are subclasses of Animal class.
• Dog is the subclass of both Mammal and Animal classes.
• Now, if we consider the IS-A relationship, we can say:
• Mammal IS-A Animal
• Reptile IS-A Animal
• Dog IS-A Mammal
• Hence: Dog IS-A Animal as well
IS-A Relationship …
• With the use of the extends keyword, the subclasses will
be able to inherit all the properties of the superclass
except for the private properties of the superclass.
• We can assure that Mammal is actually an Animal with
the use of the instance operator.
• Example:
HAS-A Relationship
• These relationships are mainly based on the usage. This
determines whether a certain class HAS-A certain
thing. This relationship helps to reduce duplication of
code as well as bugs.
• E.g.

This shows that class Van HAS-A Speed.


By having a separate class for Speed,
we do not have to put the entire code
that belongs to speed inside the Van class,
which makes it possible to reuse
the Speed class in multiple applications
HAS-A Relationship…
• In Object-Oriented feature, the users do not need to
bother about which object is doing the real work. To
achieve this, the Van class hides the implementation
details from the users of the Van class. So, basically
what happens is the users would ask the Van class to do
a certain action and the Van class will either do the
work by itself or ask another class to perform the action.
Types of Inheritance
• In Java, there are mainly three types of
inheritances Single, Multilevel, and Hierarchical.
• Java does not support Multiple and Hybrid inheritance.
• They are described as in the next figures:
Types of Inheritance…
POLYMORPHISM
• Polymorphism means "many forms", and it occurs when we
have many classes that are related to each other by
inheritance.
• Like we specified in the previous chapter; Inheritance lets us
inherit attributes and methods from another class.
• Polymorphism uses those methods to perform different tasks.
This allows us to perform a single action in different ways.
• For example, think of a superclass called Animal that has a
method called animalSound(). Subclasses of Animals could be
Pigs, Cats, Dogs, Birds - And they also have their own
implementation of an animal sound (the pig oinks, and the cat
meows, etc.):
Polymorphism: Example
Polymorphism: Example…
• Based on the previous example, we can create Pig and
Dog objects and call the animalSound() method on
both of them as follows:

You might also like