OOP Abstract Classes - Interface Polymorphysm
OOP Abstract Classes - Interface Polymorphysm
1
Outlines
polymorphism
• Defining polymorphism and its benefits
• Using inheritance to create polymorphic references
Abstraction (Abstract Classes and Interfaces)
• special types of classes (abstract classes) along with inheritance to create
polymorphic references
• interfaces to create polymorphic references
2
Introduction to Polymorphism
• Polymorphism is a object oriented programming feature that allows us to perform a single
action in different ways
• Inheritance allows you to define a base class and derive classes from
the base class
• Polymorphism allows you to make changes in the method definition for the derived classes and
have those changes apply to methods written in the base class
3
Polymorphism
For example, lets say we have a class Animal that has a method animalSound(),
here we cannot give implementation to this method as we do not know which Animal class
would extend Animal class.
So, we make this method abstract like this
Now suppose we have two Animal classes Dog and Lion that extends Animal class. We can
provide the implementation detail there
4
Polymorphism
public class Lion extends Animal{ public class Dog extends Animal{
... ...
@Override @Override
public void animalSound(){ public void animalSound(){
System.out.println("Roar"); System.out.println("Woof");
} }
} }
and
5
Polymorphism Example
Example: Quadrilaterals
If Rectangle is derived from Quadrilateral, then a Rectangle object is a more specific
version of a Quadrilateral.
Any operation that can be performed on a Quadrilateral can also be performed on a
Rectangle.
These operations can also be performed on other Quadrilaterals, such as Squares,
Parallelograms and Trapezoids.
Polymorphism occurs when a program invokes a method through a superclass Quadrilateral
variable.
At execution time, the correct subclass version of the method is called, based on the
of the reference stored in the superclass variable. 5
type
Polymorphism: Overriding
AMethods
subclass can modify the behavior inherited from a parent class
A subclass can create a method with different functionality than the parent's method but
with the:
• Same name
• Same argument list
• Almost the same return type
Invoking a method of a subclass object via a superclass reference invokes the
subclass functionality
The type of the referenced object, not the type of the variable, determines which
method is
called 6
Polymorphism: Overriding
AMethods
program can create an array of superclass variables that refer to objects of many
subclass types.
Allowed because each subclass object is an object of its superclass
The Java compiler does allow the assignment of a superclass reference to a subclass
variable if you explicitly cast the superclass reference to the subclass type.
A technique known as downcasting that enables a program to invoke subclass methods
that are not in the superclass.
8
Case Study: Payroll System
This example demonstrates that an object of a subclass can be treated as an object of
its superclass, enabling various interesting manipulations.
Problem statement
4 types of employees, paid monthly
Salaried (fixed salary, no matter the hours)
Hourly (overtime [>40 hours] pays time and a half)
Commission (paid percentage of sales)
Base-plus-commission (base salary + percentage of sales)
9
Case Study: Payroll System Using Polymorphism
Employee
BasePlusCommissionEmployee
10
Overriding Methods
11
Overriding Methods
.
.
.
12
Polymorphism: Overriding
Methods
13
Overriding Methods
.
.
.
14
Invoking Overridden Methods
Base Plus Commission
Commission reference and
reference and Object
Base Plus Commission Object
14
Invoking Overridden Methods
15
Introduction
In OOP, Abstraction is a process of hiding the implementation details from the user,
only the functionality will be provided to the user.
In other words user will have the information on what the object does instead of how it
does it.
In Java Abstraction is achieved using Abstract classes, and Interfaces
Introduction to Abstract Classes and meth
A class which contains the abstract keyword in its declaration is known as abstract class.
To use an abstract class you have to inherit it from another class, provide
implementations to the abstract methods in it.
May contain data and non-abstract methods as
well
One or more methods may be declared, but not
implemented.
• The programmer has not yet written code for a
few methods.
The
An declared and unimplemented
abstract classes are designed methods and
for subclassing
classes have the keyword abstract in their
signature.
Abstract Methods
An abstract method is one with keyword abstract in its declaration, as in
public abstract void draw(); // abstract method
A method labeled abstract is declared but not implemented
Constructors and static methods cannot be declared abstract.
A class that contains abstract methods must be an abstract class even if that class
contains some concrete (non abstract) methods
19
Abstract Classes Example
package AbstarctEample;
public abstract class Shape {
private int myX, myY;
public Shape(int x, int y) {
myX = x; myY = y;
}
public int getX() { return myX; }
public int getY() { return myY; }
public abstract double getArea(); //declaring Abstract methods
public abstract double getPerimeter();}
Shape class cannot be instantiated
all classes that extend Shape must implement getArea() and getPerimeter() or else
must also be declared abstract
Abstract classes are very useful when most or even some part of implementation
Extending an abstract class
public class Rectangle extends Shape {
private int myWidth, myHeight;
22
Interface
s interface is a blueprint of a class, which can be declared by using
An
interface keyword
Interfaces offer a capability requiring that unrelated classes implement a
set of common methods..
Properties
Define types
Declare a set of methods (no implementation!)
ADT – Abstract Data Type
Will be implemented by classes
Creating and Using
Interfaces
Interface
Definition begins with interface keyword
Classes implement an interface
Class must define every method with the number of arguments and return
type specified in the interface
If any methods are undefined, the class is abstract and must be declared so
Contains public abstract methods
Classes (that implement the interface) must implement these methods
A class can implement more than one interface
24
Implementing
Interfaces
Public interface Drivable
{
public void start();
Public void forward();
public void turn(double angle);
public void stop();
}
25
Implementing
Interfaces
26
Implementing
Interfaces
27
Implementing
Interfaces
Interface
implements
extends
28
Implementing
Interfaces
29
Implementing
Interfaces
30
Implementing Multiple
Interfaces
31
Implementing
Interfaces
The interface contains method declarations and may contain constants
All the methods are public (even if the modifier is missing)
Interfaces are pure abstract classes → cannot be instantiated
The implementer classes should implement all the methods declared in the
interface
A class can extend a single class but may implement any number of
interfaces
32
Interfaces vs.
Classes
Interface:
User-defined type
Set of methods
No implementations provided
Cannot be instantiated
Class:
User-defined type
Set of data and methods
All the methods are implemented
49
Can be instantiated
Polymorphic Argument
public class Utils{
50
Interfaces vs. Abstract
Classes
51
Interfaces vs. Abstract
Classes
???
52