0% found this document useful (0 votes)
8 views17 pages

Interfaces

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views17 pages

Interfaces

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

By Rahul Barve

Objectives
 Introduction to Interfaces
 Why Interfaces
 Interface Example
 Understand Interface Rules.
 Abstract Class Vs. Interface
 New Features

By Rahul Barve
Interface

By Rahul Barve
Interface
 Interface is a collection of abstract methods and
possibly final variables.
 Used to declare methods where implementation is not
available.

By Rahul Barve
Interface
 An interface is declared using a keyword
interface.
 E.g.
public interface MyInterface{
int myVar = 100;
void myMethod();
}

By Rahul Barve
Interface
 Once an interface is created, it can be further used by
creating an implementation class for that interface.

By Rahul Barve
Interface
 E.g.
public class MyClass
implements MyInterface {
public void myMethod(){
System.out.println(myVar);
}
}

By Rahul Barve
Why Interface

By Rahul Barve
Why Interface
 An interface is used to expand the scope of
polymorphism.
 Achieving multiple inheritance in the context of
methods.
 Used for loose coupling.

By Rahul Barve
Interface Rules
 Possible associations:
 Class extends Class
 Class implements Interface(s)
 Interface extends Interface(s)

By Rahul Barve
Interface Rules
 Methods of interface are by default public and
abstract
 Variables of interface are by default public,
static and final .
 A class that implements interface, must implement all
the methods of that interface; otherwise must be
declared abstract.

By Rahul Barve
Interface Rules
 An object of a class is always compatible with the
interface type.
 An interface type is always compatible with Object.

By Rahul Barve
Abstract Class Vs. Interface
 A class can extend only  A class can implement any
one abstract class. no of interfaces.
 Useful to achieve  Useful to achieve
polymorphism when polymorphism when
classes are co-related. classes are not co-related.
 Can contain concrete  Generally contains only
methods also. abstract methods.

By Rahul Barve
New Features

By Rahul Barve
New Features
 Since JDK 1.8, it is possible to define methods within
an interface provided they are declared as either
default or static.
 This feature enables to add a new functionality in the
interfaces without breaking the existing contract of the
implementing classes.

By Rahul Barve
Default and Static Methods
 E.g.
public interface MyInterface {
default void m1(){
//Some Code
}
static void m2(){

}
}
By Rahul Barve
Lets Summarize
 What are Interfaces
 Why Interfaces
 Interface Examples
 Interface Rules.
 Abstract Class Vs. Interface
 New Features

By Rahul Barve

You might also like