6001104-3 Structured Programming
L. Rafika Maaroufi
Chapter 7: Endomorphism,
Abstract & Interface in Java
Outline
Polymorphism Abstract
Interface
Polymorphism in java
Polymorphism comes from Greek meaning “many forms.”
Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways.
In the context of object oriented design, polymorphism refers to the ability of a reference variable to take different forms.
In particular, polymorphism enables us to write programs that process objects that share the same superclass in a class
hierarchy as if they are all objects of the superclass.
Polymorphism in java
polymorphism allows you define one interface and have multiple implementations.
There are two types of polymorphism in java:
Runtime polymorphism (Static Polymorphism)
Compile time polymorphism (Dynamic Polymorphism)
Compile time Polymorphism
Polymorphism that is resolved during compiler time is known as static polymorphism.
Example:
Method overloading
Method overloading:
is a feature that allows a class to have more than one
method having the same name, if their argument lists are different
Example of Compile time Polymorphism
T
Runtime Polymorphism
It is also known as Dynamic Polymorphism
is a process in which a call to an overridden method is resolved at runtime.
Example:
Method Overriding
Method Overriding:
Declaring a method in sub class which is already present in parent class
Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class
Runtime Polymorphism
Rules for Java Method Overriding:
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance).
Example of Runtime Polymorphism (1/3)
Consider a scenario where Bank is a class that provides
functionality to get the rate of interest. However, the rate of interest varies according to banks.
For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of interest.
Example of Runtime Polymorphism (2/3)
T
Example of Runtime Polymorphism (3/3)
T
Abstract Classes
Abstract class: a class that is declared using “abstract”
keyword.
It can have abstract methods(methods without body) An abstract class can not be instantiated, which means you are not allowed to create an object of it.
A class has an abstract method, it must be declared class
abstract.
Abstract class declaration
//Declaration using abstract keyword abstract class A{
//This is abstract method abstract void myMethod();
//This is concrete method with body void anotherMethod(){
//Does something }
}
Basics and example of abstract method.
1)
Abstract method has no body.2) Always end the declaration with a semicolon(;).
3) It must be overridden. An abstract class must be extended and in a same way abstract method must be overridden.
Why we need an abstract class?
Example:
We have a class Animal that has a method sound() and the subclasses of it like Dog, Lion, Horse, Cat etc.
Since the animal sound differs from one animal to another, there is no point to implement this method in parent class.
This is because every child class must override this method to give its own implementation details, like Lion class will say “Roar” in this method and Dog class will say “Woof”.
Why we need an abstract class? (cont.)
Interface in java
Abstract class is used for achieving partial abstraction An interface is used for full abstraction
Interface looks like a class but it is not a class.
An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body).
The variables declared in an interface are public, static & final by default
Interface in java (cont.)
Interface cannot be declared as private, protected or transient.
All the interface methods are by default abstract and public.
While providing implementation in class of any method of an interface, it needs to be mentioned as public.
implements keyword is used by classes to implement an interface.
The use of interface in Java
The interface is used for full abstraction
Java programming language does not allow you to extend
more than one class, However you can implement more than one interfaces in your class.
In java, multiple inheritance is not allowed, however you can use interface to make use of it as you can implement more than one interface.
The class that implements interface must implement all the methods of that interface
Interface declaration in java (cont.)
Interfaces are declared by specifying a keyword “interface”.
Syntax :
interface MyInterface {
//All the methods are public abstract by default //As you see they have no body
public void method1();
public void method2();
}