Professional Documents
Culture Documents
Chapter 5
Polymorphism and Interfaces
Implementation of Polymorphism
Abstract Classes
Interface
12/11/2015 Department of ECE, College of Technology 2
Introduction to Polymorphism
One of the most powerful benefits of inheritance is that, it allows a
The ability for the same code to be used with several different types
“morph” (form)
Method Overriding
Polymorphism is made possible by the fact that the type of a reference variable(
one that refers) to an object does not have to exactly match the type of object it
refers to.
The ability for superclass variables to refer to subclass objects allow us to write
flexible code that can interact with many type of objects in the same way.
how:
determines which version of that method to execute based upon the type of the
It is the type of the object being referred to (not the type of the reference
executed.
t?
class Sub1 extends Sup{
pu
void who(){
t
ou
Sup supRef;
System.out.println(“who() in Sub1”);
he
supRef = superob;
}
et
supRef.who();
b
}
ill
supRef = subob1;
tw
class Sub2 extends Sub1{
supRef.who();
ha
void who(){
supRef = subob2;
W
System.out.println(“who() in Sub2”);
supRef.who();
}
}
}
}
Such a class determines the nature of the methods that the subclasses
must implement but does not provide an implementation itself.
1. You can make the method in the superclass to simply report a warning
message
In cases where you want to ensure that a subclass does, indeed, override all
mechanism
2. Provide an abstract method in place of the method to be overridden.
constructors.
12/11/2015 Department of ECE, College of Technology 11
What is an abstract class ?
An abstract class is a class that contains one or more abstract methods in its definition.
To declare a class as an abstract class add the abstract modifier before the keyword class
You must always override that abstract method in all of your subclasses that extend the
superclass.
final.
overridden.
You can prevent a class from being inherited by preceding its declaration with final.
2. There are also situations in which you want is-a relationships and
Solution:
Java provides a feature called Interface that can represent a common
perform but does not specify how the operations are performed.
implementations.
An interface is like a class, but it contains only method headers without bodies.
However, an interface does not have instance fields but can contain
A class can use an interface by promising to implement the interface, meaning that
the class promises to provide implementations of all the methods that are
Classes that implement an interface form an is-a relationship with that interface.
method signatures
}
rov ided
is p
n t at io n
p leme
No im
E.g.
double getMeasure();
n interface
The method of a
}
We give the interface a header with the keyword interface in place of the keyword class
E.g. public interface Shape {
// …}
All interfaces are declared either as public or without an access modifier( in this case
the interface will only be available to classes inside the same package as the interface).
Methods in an interface are declared using only their return type and signature. They
Variables declared in an interface are not instance variables. Instead they are implicitly