Interface
Interface
Presented by
Ms.G.Sakthi Priya, AP/CSE
Interface
• An interface in java is a blueprint of a class.
• The interface in java is a mechanism to achieve abstraction and multiple
inheritance
• Interface is similar to a class which contains method’s signature only but
not implementation.
• It has a formal set of method and constant declarations that must be
defined by the class which implements it.
• Interface is declared by using interface keyword. It provides total
abstraction (100% abstraction)
• All the methods declared in the interface are public and abstract
methods by default.
• All the data members are public, static, final members.
• Constructors are not allowed in interface
• We can’t create instance(interface can’t be instantiated) of interface
but we can make reference of it that refers to the Object of its
implementing class
Key points to remember about
interfaces
• Interface provides full abstraction as none of its methods have body.
On the other hand abstract class provides partial abstraction as it can
have abstract and concrete(methods with body) methods both
• “implements” keyword is used by classes to implement an interface.
• While providing implementation in class of any method of an
interface, it needs to be mentioned as public
• Class that implements any interface must implement all the methods
of that interface, else the class should be declared abstract
• Interface cannot be declared as private, protected
Defining Interface
Implementing Interface
• Once an interface has been defined, one or more classes can
implement that interface.
• To implement an interface, include the implements clause in a class
definition, and then create the methods required by the interface
Simple Example using Interface
Output
interface Vehicle{
void running(); //by default public abstract Running
}
class car implements Vehicle{
public void running(){
System.out.println("Running Car");
} Accessing implementation
} through
class Demo{ interface reference
public static void main(String args[]){
car c = new car(); car c = new car(); Vehicle v;
v=new car();
c.running(); Vehicle v;
v=c; v.running();
}
v.running(); All the three ways are valid and
}
produces the same result
Interface with variable
interface Vehicle{
int a=10; //By default public static final
void run();
}
class car implements Vehicle{
public void run(){
System.out.println("Running");
//a=20; cannot assign a value to final variable a
System.out.println(a);
}
}
class Demo{
public static void main(String args[]){
Output
Vehicle v; Running
v=new car(); 10
v.run();
}
}
Exp 6
• Write a Java Program to create an abstract class named Shape that
contains two integers and an empty method named printArea().
Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape. Each one of the
classes contains only the method printArea( ) that prints the area of
the given shape.
• Solve the above problem using an interface.
Design an interface Appliance with methods turnOn() and turnOff().
Implement this interface in classes Fan, Light, and AirConditioner. Each
appliance should print a message indicating whether it's turned on or
off.
Relationship between classes and
interface
• Any class can extend only 1 class (that’s why multiple inheritance is not
possible)
• Any class can implement an infinite number of interface
• An interface can extend another interface or interfaces (more than one
interface) only.
Multiple inheritance
Class A Class B
extends
Class C
} k.playwithchild();
interface Mother{ k.teachchild();
void playwithchild(); }
void teachchild();
}
}
class kid implements Father,Mother{
public void playwithchild(){
System.out.println("Father and Mother playing with child");
}
public void teachchild(){
System.out.println("Mother is teaching the child");
}
}
Class poll
Output
Implement meth1().
Implement meth2().
Implement meth3().
// Implement the third interface in a class
class MyClass implements C {
// Define the first interface
public void methodA() {
interface A { System.out.println("Method A");
Example 2: Multiple Inheritance
void methodA(); }
}
public void methodB() {
// Define the second interface System.out.println("Method B");
interface B { }
void methodB();
} public void methodC() {
System.out.println("Method C");
interface C extends A, B { }
void methodC(); }
}
public class Demo {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.methodA();
obj.methodB();
obj.methodC();
}
}
Class poll
• Is the below representation is possible?
An abstract class can be extended using keyword Java interface can be implemented using keyword
“extends”. “implements”.
Abstract class doesn't support multiple Inheritance Interface supports multiple inheritance.
The abstract keyword is used to declare abstract class The interface keyword is used to declare interface