Interface
Interface
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 ?
class
Employee{getYearlyPay()
=12*MonthlyPay;}
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();}}