Lesson 25
Interface
An interface defines abstract methods and constants. Classes implement interfaces to achieve full abstraction and multiple inheritance.
Java Track
Save progress as you learn.
61 lessons
Lesson content
What is an Interface in Java?
An interface looks similar to a class, but it is not a class. It contains method declarations and constants that can be implemented by one or more classes.
- Methods declared in an interface are abstract by default.
- Variables declared in an interface are constants (public static final).
- Interfaces are widely used to achieve full abstraction and improve code flexibility.
Definition List
- Interface: A blueprint that contains abstract methods and constants, which must be implemented by a class.
- Abstract Method: A method that has only a declaration and no implementation.
- Implement: The process of providing the body for methods declared in an interface.
- Multiple Inheritance: A feature where a class can inherit behavior from multiple interfaces.
- Constant: A value that cannot be changed after initialization.
Why Use Interfaces in Java?
- Achieve abstraction
- Support multiple inheritance
- Improve code reusability
- Increase flexibility
- Promote loose coupling between classes
Syntax of an Interface
Defining an Interface
interface MyInterface {
datatype variable = constant;
public void method1();
public void method2();
}Implementing an Interface
class ClassName implements InterfaceName {
// body of class
}Key Characteristics of Interfaces
- Cannot be instantiated directly.
- Methods are abstract by default.
- Variables are constants by default.
- Supports multiple inheritance.
- Can be implemented by multiple classes.
- Used to achieve full abstraction.
Example 01: Simple Interface in Java
interface Animal {
void Sound();
void sleep();
}
class Dog implements Animal {
@Override
public void Sound() {
System.out.println("The Dog Sounds like : woof");
}
@Override
public void sleep() {
System.out.println("Dog Sleeping");
}
}
public class interfaceDemo {
public static void main(String args[]) {
Dog o = new Dog();
o.Sound();
o.sleep();
}
}Explanation
- interface Animal defines two methods: Sound() and sleep().
- class Dog implements Animal and provides the method bodies.
- Dog o = new Dog() creates the object and calls the implemented methods.
Output
The Dog Sounds like : woof
Dog SleepingMultiple Inheritance Using Interfaces
Java does not support multiple inheritance through classes. However, a class can implement multiple interfaces, which is one of the biggest advantages of interfaces.
Example 02: Multiple Inheritance Using Interfaces
class Phone {
void voiceCall() {
System.out.println("Make VoiceCall");
}
void sms() {
System.out.println("We Can send SMS");
}
}
interface Camera {
void click();
void record();
}
interface player {
void play();
void pause();
void stop();
}
class SmartPhone extends Phone implements Camera, player {
@Override
public void click() {
System.out.println("Take a Selfi");
}
@Override
public void record() {
System.out.println("Take a video");
}
@Override
public void play() {
System.out.println("Play Music");
}
@Override
public void pause() {
System.out.println("Pause Music");
}
@Override
public void stop() {
System.out.println("Stop Music");
}
}
public class interfaceDemo2 {
public static void main(String[] args) {
SmartPhone o = new SmartPhone();
o.voiceCall();
o.sms();
o.click();
o.record();
o.play();
o.pause();
o.stop();
}
}Explanation
- Phone is a normal class.
- Camera is an interface.
- player is another interface.
- SmartPhone inherits from Phone and implements both Camera and player.
Output
Make VoiceCall
We Can send SMS
Take a Selfi
Take a video
Play Music
Pause Music
Stop MusicInterface Variables as Constants
Variables declared inside an interface are automatically public static final. Their values cannot be modified.
Example 03: Interface Constants
interface circle {
double pi = 3.14;
double radius = 5.5;
void compute();
}
class area implements circle {
public void compute() {
double result = pi * radius * radius;
System.out.println("Area of Circle=" + result);
}
}
class circumference implements circle {
public void compute() {
double result = 2 * pi * radius;
System.out.println("Circumference of Circle=" + result);
}
}
class interfacedemo {
public static void main(String args[]) {
area a = new area();
a.compute();
circumference c = new circumference();
c.compute();
}
}Explanation
The circle interface contains constants pi and radius and abstract method compute(). Both area and circumference implement their own versions of compute().
Output
Area of Circle=94.985
Circumference of Circle=34.54Interface vs Abstract Class
- Object Creation — Both: Not Allowed.
- Abstract Methods — Both: Yes.
- Normal Methods — Interface: Limited (Java 8+). Abstract Class: Yes.
- Variables — Interface: Constants only. Abstract Class: Any type.
- Multiple Inheritance — Interface: Supported. Abstract Class: Not Supported.
- Keyword — Interface: interface. Abstract Class: abstract.
Advantages of Interfaces in Java
- Supports Multiple Inheritance: A class can implement multiple interfaces simultaneously.
- Provides Full Abstraction: Only method declarations are exposed.
- Enhances Flexibility: Different classes can implement the same interface differently.
- Improves Reusability: Common behavior can be defined once and reused.
- Promotes Loose Coupling: Classes depend on interfaces rather than specific implementations.
FAQ
1. What is an interface in Java?
An interface is a blueprint that contains abstract methods and constants which must be implemented by a class.
2. Can a Java class implement multiple interfaces?
Yes. A Java class can implement multiple interfaces, which enables multiple inheritance.
3. What is the difference between an interface and an abstract class?
An interface primarily provides abstraction and supports multiple inheritance, whereas an abstract class can contain both abstract and non-abstract methods but does not support multiple inheritance.
Where Java is used
Java in real-world development
Interface
Blueprint of a class. Contains abstract methods and constants only.
Abstract Methods
Methods with no body. Implementing class must provide the implementation.
Constants
Variables in an interface are public, static and final by default.
implements Keyword
Used by a class to implement an interface. All methods must be overridden.
Multiple Interfaces
A class can implement more than one interface, achieving multiple inheritance.
Full Abstraction
Interfaces provide 100% abstraction since all methods are abstract.