0% found this document useful (0 votes)
8 views

05 - Polymorphism

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

05 - Polymorphism

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

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;
}

[501043 Lecture 7: ADT]


7
Abstract Classes
• Syntax
abstract class A
{
//attributes;
//methods;
abstract void printStatus();//no method body and abstract
}

[501043 Lecture 7: ADT]


8
Abstract Classes
abstract class Bike
{
abstract void run();
}
class Honda4 extends Bike
{
void run(){System.out.println("running safely");}
public static void main(String args[])
{
Bike obj = new Honda4();
obj.run();
}
}

[501043 Lecture 7: ADT]


9
Abstract Classes…

Modified
Abstract Classes…

This class have no abstract method but it is declared as an


abstract class. So, we can not initiate an object of this class.
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

Overloaded methods: Methods have the same


name but their parameters are different in a class
How Can Overridden Method be
Determined?
How Can Overridden Methods be
Determined?
Son(declaration)
int y=2
Method Table
(m1, 600)
(m2, 800)
800 m2()
700

Father(declaration) Class: 700


int x=0 Class: 500 y=2
Method Table 8000
x=0 x=0
9000
(m1, 600)
(m2, 650)
650 m2()
600 m1() obj: 8000 obj: 9000

500
obj.m1(); obj.m1();
Classes are loaded to obj.m2(); obj.m2();
static heap
WHY AND WHEN TO USE
INTERFACES?

• To achieve security - hide certain details and only


show the important details of an object (interface).
• Java does not support "multiple inheritance" (a class
can only inherit from one superclass). However, it
can be achieved with interfaces, because the class
can implement multiple interfaces
Interfaces…
Interfaces…

m3(), m4() in A
cannot implement
m3(), m4() in
InterfaceDemo,
attempting to assign
weaker access
privileges, were
public

Default methods of an interface must be overridden as public methods in


concrete classes.
Interfaces

Interfaces
• An interface is a reference type, similar to a
class, that can contain only constants, initialized
fields, static methods, prototypes (abstract
methods, default methods), static methods, and
nested types.
• It will be the core of some classes
• Interfaces cannot be instantiated because they
have no-body methods.
• Interfaces can only be implemented by classes
or extended by other interfaces.
Anonymous Classes
Anonymous classes are classes which are not named but
they are identified automatically by Java compiler.
Where are they? They are identified at initializations of
interface/abstract class object but abstract methods are
implemented as attachments.
Why are they used?
• Enable you to make your code more concise.
• Enable you to declare and instantiate a class at the same
time.
• They are like local classes except that they do not have a
name.
• Use them if you need to use a local class only once.
Anonymous Class…

Anonymous
class.

Class name is given by the


compiler:
ContainerClass$Number
Anonymous Class…
Concrete methods but
they can not be used
because the class is
declared as abstract one.

The abstract class can


be used only when at
least one of it’s
methods is overridden

Anonymous class is a technique is commonly used to


support programmer when only some methods are
overridden only especially in event programming.
Nested Class 1-2
• Java allows defining a class within another class.
• Such a class is called a nested class as shown in the following figure:


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:

Creates logical grouping of classes


• If a class is of use to only one class, then it can be embedded within that class and
the two classes can be kept together.
• In other words, it helps in grouping the related functionality together.
• Nesting of such ‘helper classes’ helps to make the package more efficient and
streamlined.

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.

Increased readability and maintainability of code


• Nesting of small classes within larger top-level classes helps to place the code
closer to where it will be used.

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

Static Nested 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

You might also like