05 - Polymorphism
05 - Polymorphism
Objectives
• Overloading and Overriding
• Abstract class
• Interface
Polymorphism
• 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 OOP.
Polymorphism
• Example
Shape
Circle Rectangle
Square Box
Abstract Classes
• Used to define what behaviors a class is
required to perform without having to
provide an explicit implementation.
• It is the result of so-high generalization
• Syntax to define a abstract class
• public abstract class className{ ... }
• It isn’t necessary for all of the methods in an
abstract class to be abstract.
• An abstract class can also declare implemented
methods.
Abstract Classes
Abstract Classes
• Syntax
abstract class A
{
//attributes;
//methods;
}
Modified
Abstract Classes…
Error.
Why?
Implementing Abstract Methods
• If there is an abstract method in a class, that
class must be abstract.
• If you are extending an abstract class that has
an abstract method, you must either provide the
implementation of the method or make this class
abstract.
• The abstract class can also be used to provide
some implementation of the interface. In such
case, the end user may not be forced to override
all the methods of the interface.
• Abstract classes cannot be instantiated
Polymorphism
Ability allows many versions of a method
based on overloading and overriding
methods techniques.
Overloading: A class can have some
methods which have the same name but
their parameter types are different.
Overriding: A method in the father class can
be overridden in its derived classes (body of
a method can be replaced in derived
classes).
Overloading
• overloading with constructors
public Rectangle(){…}
Rectangle public Rectangle(int length, int width){… }
# length: int
# width: int • Overloading also extends to
+ Rectangle();
general methods.
+ Rectangle(int, int) public void setValue(int len){
+ setValue(int): void length= (len>0)?1:0;
+ setValue(int, int): void }
public void setValue (int len, int wi){
length= (len>0)? 1: 0;
width= (wi>0)? wi:0;
}
Overriding
Rectangle
# length: int
# width: int
+ Rectangle();
+ Rectangle(int, int)
+ setValue(int ):void
+ setValue(int ,int ):void
+ toString(): String
Box
Height: int
+ Box();
+ Box(int, int, int)
+ toString(): String
+ set(int, int, int ): void
Overriding Inherited Methods
Overridden method: An inherited method is re-written
500
obj.m1(); obj.m1();
Classes are loaded to obj.m2(); obj.m2();
static heap
WHY AND WHEN TO USE
INTERFACES?
m3(), m4() in A
cannot implement
m3(), m4() in
InterfaceDemo,
attempting to assign
weaker access
privileges, were
public
Anonymous
class.
Following code snippet defines a nested class:
class Outer{
...
class Nested{
...
}
}
The class Outer is the external enclosing class and the class Nested is the class
defined within the class Outer.
28
•
Nested Class 2-2
Nested classes are classified as static and non-static.
• Nested classes that are declared static are simply termed as static nested
classes whereas non-static nested classes are termed as inner classes.
• This has been demonstrated in the following code snippet:
class Outer{
...
static class StaticNested{
...
}
class Inner{
...
}
}
The class StaticNested is a nested class that has been declared as static whereas the
non-static nested class, Inner, is declared without the keyword static.
A nested class is a member of its enclosing class.
Non-static nested classes or inner classes can access the members of the enclosing
class even when they are declared as private.
Static nested classes cannot access any other member of the enclosing class.
29
Benefits of Using Nested Class
The reasons for introducing this advantageous feature of defining nested class in Java
are as follows:
Increases encapsulation
• In case of two top level classes such as class A and B, when B wants access to
members of A that are private, class B can be nested within class A so that B
can access the members declared as private.
• Also, this will hide class B from the outside world.
• Thus, it helps to access all the members of the top-level enclosing class even if
they are declared as private.
30
Types of Nested Classes
The different types of nested classes are as follows:
Member classes or
non-static nested classes
Local classes
Anonymous classes
31
Member Classes
• A member class is a non-static inner class.
• It is declared as a member of the outer or enclosing class.
• The member class cannot have static modifier since it is
associated with instances of the outer class.
• An inner class can directly access all members that is, fields
and methods of the outer class including the private ones.
• However, the outer class cannot access the members of the
inner class directly even if they are declared as public.
• This is because members of an inner class are declared
within the scope of inner class.
• An inner class can be declared as public, private, protected,
abstract, or final.
• Instances of an inner class exist within an instance of the
outer class.
• To instantiate an inner class, one must create an instance of
the outer class. 32
Local Class
• An inner class defined within a code block such as the body
of a method, constructor, or an initializer, is termed as a
local inner class.
• The scope of a local inner class is only within that particular
block.
• Unlike an inner class, a local inner class is not a member of
the outer class and therefore, it cannot have any access
specifier.
• That is, it cannot use modifiers such as public, protected,
private, or static.
• However, it can access all members of the outer class as
well as final variables declared within the scope in which it
is defined.
33
Local Class
Following figure displays a local inner class:
34
Anonymous Class
Following figure displays an anonymous class:
35
Static Nested Class
A static nested class is associated with the outer class just like
variables and methods.
A static nested class cannot directly refer to instance variables
or methods of the outer class just like static methods but can
access only through an object reference.
A static nested class, by behavior, is a top-level class that has
been nested in another top-level class for packaging
convenience.
Static nested classes are accessed using the fully qualified
class name, that is, OuterClass.StaticNestedClass.
A static nested class can have public, protected, private,
default or package private, final, and abstract access
specifiers.
Following code snippet demonstrates the use of static nested
class: 36
Summary
• Polymorphism is a concept of object-oriented
programming
• Polymorphism is the ability of an object to take on
many forms
• Overloading and overriding are a technology to
implement polymorphism feature.
• In OOP occurs when a parent class/ interface
reference is used to refer to a child class object