You are on page 1of 20

INTERFACE&

POLYMORPHISM
INTERFACES

Java does not allow multiple inheritance(i.e) a subclass being the extension of more than one
superclass
An interface defines a protocol of behavior
The aim of interfaces in Java is to dictate common behavior among objects from diverse classes
For that purpose an interface declares a set of so called abstract methods i.e methods with an
empty body
An interface can also declare constants
A class that implements the interface agrees to implement all methods defined in the interface
thereby agreeing to a certain behavior
INTERFACES
Like a class, an interface can have methods and variables, but the methods declared in an interface
are by default abstract (only method signature, no body).  
Interfaces specify what a class must do and not how. It is the blueprint of the class.
Dictates common behavior among objects from diverse classes and declares methods which are
expected to be implemented in a class
If a class implements an interface and does not provide method bodies for all functions specified in
the interface, then the class must be declared abstract.
IMPLEMENTATION OF
INTERFACE
A class can implement one or more interfaces using keyword implements in the class
declaration followed by the comma-separated list of interface names
class name_name implements interface_1, interface_2,……{
interface_body
}
Eg: if class Car that implements CarInterface ,then Car inherits all constants from the
interface and has to implement the methods honk(),go() and brake()
Note: The methods have to be declared as public as they were declared public in the
interface dimension
IMPLEMENTATION OF
INTERFACE
An interface can extend other interfaces,
just as class can extend only one other class
an interface can extend any number of interfaces
IMPLEMENTATION OF
INTERFACES
WHY DO WE USE INTERFACE ?

•It is used to achieve total abstraction.


•Since java does not support multiple inheritance in case of class, but by using
interface it can achieve multiple inheritance .
•Interfaces are used to implement abstraction. So the question arises why use
interfaces when we have abstract classes?
•The reason is, abstract classes may contain non-final variables, whereas variables in
interface are final, public and static and methods are public and abstract.,
INTERFACE EXAMPLE
interface FirstInterface {
public void myMethod(); // interface method
}
interface SecondInterface {
public void myOtherMethod(); // interface method
} Some text...
Some other text...
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text.."); }
public void myOtherMethod() {
System.out.println("Some other text...");
}}
class Main {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();}}
interface A {
void funcA();
}
interface B extends A {
void funcB();
}
class C implements B {
public void funcA() {
System.out.println("This is funcA");}
public void funcB() {
System.out.println("This is funcB"); }}
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
}
}
INTERFACES VS.MULTIPLE
INHERITANCE
No variables (except constants) can be inherited from interfaces.
An interface cannot implement any methods
An interface is not part of the class hierarchy ;unrelated classes can implement the
same interface.,
To share code among classes, its better to put the code inside a superclass,An
interface cannot contain code.,
POLYMORPHISM IN JAVA
Polymorphismmany shapes or many forms
Within object oriented languages it is used to denote one name referring to several different
methods
Within Java there are two different types of Polymorphism
Overloading
Overriding
OVERLOADING
Ability to define several methods to have the same name within a particular class
long Math.max(long a,long b)
double Math.max(double a,double b)
int Math.max(int a,int b)
OVERRIDDING
Overriding happens whenever a subclass has a method with the same signature(number,type and order of parameters)
as a method in one of its superclasses.
When this happens, the method in the derived class overrides the method in the superclass

class
Employee{getYearlyPay()
=12*MonthlyPay;}

Class Programmer{ Class


getYearlyPay()=same as SalesPerson{getYearlyPay()= as
Employee} Employee+bonus

Polymorphic methods behave differently when it is invoked on different objects


Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon
DYNAMIC BINDING
Refers to case where compiler is not able to resolve the call
Binding is done at runtime only
At compile time, the compiler cant be sure if the call to the method someMethod() on these references
actually refers to which version of the method –the super class version or the sub class version., Such
methods are known as Polymorphic methods
Dynamic binding simply binds the method calls based on the actual object and not on the declared
type of the object reference

A Polymorphic method is a method that behaves differently when it is invoked on different objects
STATIC BINDING OR EARLY
BINDING
If compiler could resolve the binding at the compile time only then such a binding is called Static
binding or Early binding
Instance method calls resolved at runtime
Static method calls(class method accessed by class name)  resolved at compile time itself and hence
we have static binding for static method calls.
That’s way static methods cannot actually be overridden
STATIC BINDING OR EARLY
BINDING
Similarly access to all the member variables in Java follows static binding as Java doesn’t support
polymorphic behaviour of member variables

The member variable is resolved based on the declared type of the object reference only, which
the compiler is able to finding as early as at the compile time only and hence a static binding in this
case.,
STATIC BINDING OR EARLY
BINDING
Another example for static binding is private methods as they are never inherited
and the compiler can resolve calls to any private method at compile time only
Polymorphism is the capability of an action or method to do different things
based on the object that it is acting upon.,
The program does not have to know the exact type of the object in advance,so this
behaviour can be implemented at runtime (this is called late binding or dynamic
binding)
WHY IS POLYMORPHISM
USEFUL?
With polymorphism it is possible to write a method that correctly processes lots of different
types of classes all within class hierarchy through a superclass reference ensuring that the
correct subclass methods will be called
Without polymorphism we may have to write the code the following way
INHERITANCE AND POLYMORPHISM
class Shapes {
public void area() {
System.out.println("The formula for area of "); }}
class Triangle extends Shapes {
public void area() { Output:
System.out.println("Triangle is ½ * base * height ");}}
The formula for the area of
class Circle extends Shapes {
Triangle is ½ * base * height
public void area() { The formula for the area of
System.out.println("Circle is 3.14 * radius * radius "); }} Circle is 3.14 * radius * radius
class Main {
public static void main(String[] args) {
Shapes myShape = new Shapes(); // Create a Shapes object
Shapes myTriangle = new Triangle(); // Create a Triangle object
Shapes myCircle = new Circle(); // Create a Circle object
myShape.area();
myTriangle.area();
myShape.area();
myCircle.area();}}
INTERFACE
interface A {
void funcA();}
interface B extends A {
void funcB();}
class C implements B {
public void funcA() {
System.out.println("This is funcA");}
public void funcB() {
System.out.println("This is funcB");}}
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();}}

You might also like