0% found this document useful (0 votes)
46 views16 pages

s10 InheritPolymorphism

Uploaded by

Le Doan
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)
46 views16 pages

s10 InheritPolymorphism

Uploaded by

Le Doan
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/ 16

Fundamentals of Java

◆ Describe inheritance
◆ Explain the types of inheritance
◆ Explain super class and subclass
◆ Explain the use of super keyword
◆ Explain method overriding
◆ Describe Polymorphism
◆ Differentiate type of reference and type of objects
◆ Explain static and dynamic binding
◆ Explain virtual method invocation
◆ Explain the use of abstract keyword

© Aptech Ltd. Inheritance and Polymorphism/Session 10 2


◆ In OOP, inheritance enables new classes to receive—or inherit—the properties
and methods of existing classes.
◆ Features:
 The class that is derived from another class is called a subclass, derived

class, child class, or extended class.


 A class that is used as the basis for inheritance is called a super class, base

class, or parent class.


 A subclass inherits all the members such as fields, nested classes, and

methods from its super class except those with private access specifier.
 Members having default accessibility in the super class are not inherited

by subclasses of other packages.


 Constructors are not considered as members of a class, so they are not

inherited. However, the child class can invoke the constructor of the super
class from its own constructor.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 3


◆ There are several types of inheritance as shown in the following figure:

• A child class • A child class


inherits from derives from a
one and only parent that itself
one parent class is a child of
another class
Single Multilevel
Inheritance Inheritance

Hierarchical Multiple
Inheritance Inheritance
• A parent class • A child class
has more than derives from
one child classes more than one
at different parent class
levels

© Aptech Ltd. Inheritance and Polymorphism/Session 10 4


can declare a field with the same name as the one in the super class → hiding of
super class field which is not advisable.

can declare new fields. These members will be specific to the subclass.

can write a new instance method with the same signature as the one in the super
class → method overriding.

can create a new static method with the same signature as the one in the super
class → hiding of the super class method.

also can declare new methods.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 5


◆ If a class does not have any super class, it is implicitly derived from Object class.
◆ The syntax for creating a subclass is as follows:

public class <class1-name> extends <class2-name>


{


}

where,
class1-name: Specifies the name of the child class.
class2-name: Specifies the name of the parent class.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 6


◆ Java allows an instance method in a subclass to have the same signature and return
type as an instance method of the super class.
◆ This is called method overriding.
◆ Rules to remember when overriding:

An overriding method cannot have a weaker access specifier than the access
specifier of the super class method.

subclass can invoke the super class constructor and methods using the keyword
super.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 7


The word polymorph is a combination of two words namely, ‘poly’ which
means ‘many’ and ‘morph’ which means ‘forms’.

Thus, polymorph refers to an object that can have many different forms.

This principle can also be applied to subclasses of a class that can define their
own specific behaviors as well as derive some of the similar functionality of the
super class.

The concept of method overriding is an example of polymorphism in object-


oriented programming.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 8


◆ Some important differences between static and dynamic binding are listed in the
following table:

Static Binding Dynamic Binding


Static binding occurs at compile time. Dynamic binding occurs at runtime.
Private, static, and final methods
Virtual methods are bounded at runtime
and variables use static binding and are
based upon the runtime object.
bounded by compiler.
Static binding uses object type information Dynamic binding uses reference type to
for binding. That is, the type of class. resolve binding.
Overloaded methods are bounded using Overridden methods are bounded using
static binding. dynamic binding.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 9


◆ Consider the context in which Employee is a parent class of PartTimeEmployee class
and then we have a declaration as following:
Employee o = new PartTimeEmployee()

◆ In the earlier code:


◆ type of object of o is Employee.
◆ However, the reference assigned to the object was of PartTimeEmployee.
◆ This means that the object will bind with the members of PartTimeEmployee
class during runtime.
◆ That is, object type is Employee and reference type is PartTimeEmployee.
◆ This is possible only when the classes are related with a parent-child relationship.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 10


Upcasting Allows casting an instance of a subclass to its parent class :

❖ For example, PartTimeEmployee oPT = new PartTimeEmployee();


Employee objEmp = oPT; // upcasting
❖ However, the parent object cannot access the members that are specific to the child
class and not available in the parent class.

Downcasting Allows casting the parent reference back to the child type.

❖ Casting a parent object to child type is called downcasting because an object is being
casted to a class lower down in the inheritance hierarchy.
❖ However, downcasting requires explicit type casting by specifying the child class name
in brackets.
❖ For example, PartTimeEmployee oPT2=(PartTimeEmployee) objEmp;

© Aptech Ltd. Inheritance and Polymorphism/Session 10 11


▪ The compiler checks the accessibility of each method and field based on the
class definition whereas the behavior associated with an object is determined
at runtime.
▪ It means, if the object created is of child class (reference type)
then the invoked method is also belonged to child class, even though the
object type is super class.
▪ This is referred to as virtual method invocation and the method is referred to as
virtual method.

◆ In other languages such as C++, the same can be achieved by using the keyword
virtual.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 12


Java provides the abstract keyword to create a super class that serves as a
generalized form that will be inherited by all of its subclasses.
The methods of the super class serve as a contract or a standard that the subclass
can implement in its own way.

Abstract method

An abstract method is one that is declared with the abstract keyword


and is without an implementation, that is, without any body.

◆ The syntax for declaring an abstract method is as follows:


abstract <return-type> <method-name> (<parameter-list>);

◆ For example,
public abstract void calculate();

© Aptech Ltd. Inheritance and Polymorphism/Session 10 13


Abstract class

An abstract class is one that consists of abstract methods.

◆ Abstract class serves as a framework that provides certain behavior for other
classes.
◆ The subclass provides the requirement-specific behavior of the existing framework.
◆ Abstract classes cannot be instantiated and they must be subclassed to use the
class members.
◆ The subclass provides implementations for the abstract methods in its parent class.
◆ The syntax for declaring an abstract class is as follows:

abstract class <class-name>


{
// declare fields
// define concrete methods
[abstract <return-type> <method-name>(<parameter-list>);]
}

© Aptech Ltd. Inheritance and Polymorphism/Session 10 14


◆ For example:

© Aptech Ltd. Inheritance and Polymorphism/Session 10 15


◆ Inheritance is a feature in Java through which classes can be derived from other classes
and inherit fields and methods from classes it is inheriting.
◆ The class that is derived from another class is called a subclass, derived class, child
class, or extended class. The class from which the subclass is derived is called a super
class.
◆ Creation of an instance method in a subclass having the same signature and return
type as an instance method of the super class is called method overriding.
◆ Polymorphism refers to an object that can have many different forms.
◆ When the compiler resolves the binding of methods and method calls at compile time,
it is called static binding or early binding. If the compiler resolves the method calls and
the binding at runtime, it is called dynamic binding or late binding.
◆ An abstract method is one that is declared with the abstract keyword without an
implementation, that is, without any body.
◆ An abstract class is one that consists of abstract methods.
◆ An abstract class serves as a framework that provides certain pre-defined behavior for
other classes that can be modified later as per the requirement of the inheriting class.

© Aptech Ltd. Inheritance and Polymorphism/Session 10 16

You might also like