OOP
Polymorphism, abstract
classes and interfaces
Lec 6
Mohammed
1
Polymorphism
• Polymorphism means "many forms", and it
occurs when we have many classes that
are related to each other by inheritance.
• Real-life Illustration: Polymorphism A
person at the same time can have
different characteristics. Like a man at the
same time is a father, a husband, an
employee. So the same person possesses
different behavior in different situations.
This is called polymorphism.
2
Polymorphism
• A polymorphic reference can refer to different types of
objects at different times
• In java every reference can be polymorphic except of
references to base types and final classes.
• It is the type of the object being referenced, not the
reference type, that determines which method is invoked
• Polymorphic references are therefore resolved at run-time,
not during compilation; this is called dynamic binding
• Careful use of polymorphic references can lead to elegant,
robust software designs
3
Polymorphism
• Polymorphism is considered one of the
important features of Object-Oriented
Programming. Polymorphism allows us to
perform a single action in different ways.
• In Java polymorphism is mainly divided into
two types:
– Compile-time Polymorphism
– Runtime Polymorphism
4
Compile-time polymorphism
• It is also known as static polymorphism.
This type of polymorphism is achieved by
function overloading.
5
Runtime polymorphism
• It is also known as Dynamic Method
Dispatch. It is a process in which a
function call to the overridden method is
resolved at Runtime. This type of
polymorphism is achieved by Method
Overriding.
6
Overloading vs. Overriding
• Overloading deals with • Overriding deals with
multiple methods in two methods, one in a
parent class and one
the same class with in a child class, that
the same name but have the same
different signatures signature
• Overloading lets you • Overriding lets you
define a similar
define a similar operation in different
operation in different ways for different
ways for different data object types
7
ABSTRACT CLASSES
8
Abstract Classes and Methods
• Data abstraction is the process of hiding
certain details and showing only essential
information to the user.
• Abstraction can be achieved with either
abstract classes or interfaces
9
Abstract classes
• Java allows abstract classes
– use the modifier abstract on a class header to
declare an abstract class
• abstract class Vehicle
{…}
• An abstract class is a placeholder in a
class hierarchy that represents a generic
concept
10
Abstract Classes and Methods
• The abstract keyword is a non-access
modifier, used for classes and methods:
– Abstract class: is a restricted class that
cannot be used to create objects (to access it,
it must be inherited from another class).
– Abstract method: can only be used in an
abstract class, and it does not have a body.
The body is provided by the subclass
(inherited from).
11
Example
public abstract class Vehicle
{
String name;
public String getName()
{ return name; } \\ method body
abstract public void move(); \\ no body!
}
12
Abstract Classes and Methods
• Example :
Public abstract class employee {
public abstract void salary();
public void name (string n) {
[Link](n) ;
}
}
• It is not possible to create an object of the abstract
class.
– employee myObj = new employee(); // will generate an error
• An abstract class can have both abstract and regular
methods
13
Abstract Classes
• The non-abstract child of an abstract class
must override the abstract methods of the
parent
• An abstract class cannot be instantiated
• The use of abstract classes is a design
decision; it helps us establish common
elements in a class that is too general to
instantiate
14
INTERFACES
15
Interfaces
• Another way to achieve abstraction in Java, is
with interfaces.
• An interface is a completely "abstract class"
that is used to group related methods with
empty bodies
• Interfaces are syntactically similar to classes,
but they lack instance variables, and their
methods are declared without any body.
16
Interfaces
• To access the interface methods, the
interface must be "implemented" by
another class with the implements
keyword.
• Interfaces are developed to support
multiple inheritance...
• The methods present in interfaces are
pure abstract..
17
Multiple Interfaces
• To implement multiple interfaces, separate
them with a comma.
18
Example
interface FirstInterface {
public void myMethod(); // interface
method
}
interface SecondInterface {
public void myOtherMethod(); // class Main {
interface method public static void
} main(String[] args) {
class DemoClass implements DemoClass myObj = new
FirstInterface, SecondInterface { DemoClass();
public void myMethod() {
[Link]("Some text..");
[Link]();
} [Link]();
public void myOtherMethod() { }
[Link]("Some other }
text...");
}
}
19
Con..
• Two types of access:
1) public – interface may be used anywhere in a program
2) default – interface may be used in the current package only
• Interface methods have no bodies – they end with the
semicolon after the parameter list.
• They are essentially abstract methods.
• An interface may include variables, but they must be
final, static and initialized with a constant value.
• In a public interface, all members are implicitly public.
20
• General format of a class that includes the
implements clause:
• Syntax:
access class name extends super-class implements
interface1, interface2, …, interfaceN {
…
}
• Access is public or default.
21
Notes on Interfaces
• Like abstract classes, interfaces cannot be
used to create objects.
• Interface methods do not have a body -
the body is provided by the "implement"
class
• On implementation of an interface, you
must override all of its methods
22
Notes on Interfaces
• Interface methods are by default abstract
and public
• Interface attributes are by default public,
static and final
• An interface cannot contain a constructor
(as it cannot be used to create objects)
23
Example
Consider interfaces A and B.
interface A {
void meth1();
void meth2();
}
B extends A:
interface B extends A {
void meth3();
}
24
Example
MyClass must implement all of A and B
methods:
class MyClass implements B {
public void meth1() {
[Link]("Implement meth1().");
}
public void meth2() {
[Link]("Implement meth2().");
}
public void meth3() {
[Link]("Implement meth3().");
}} 25
Example
Create a new MyClass object, then invoke all
interface
methods on it:
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
26
Any questions?
27