0% found this document useful (0 votes)
2 views8 pages

Interfaces in Java-1

An interface in Java serves as a blueprint for classes, defining what methods a class should implement without providing the implementation. It allows for multiple inheritance, enabling a class to implement multiple interfaces, and supports abstract methods, static constants, and, since Java 8, static and default methods. Key differences between interfaces and abstract classes include the absence of instance variables and constructors in interfaces, and the ability to have multiple inheritance through interfaces.

Uploaded by

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

Interfaces in Java-1

An interface in Java serves as a blueprint for classes, defining what methods a class should implement without providing the implementation. It allows for multiple inheritance, enabling a class to implement multiple interfaces, and supports abstract methods, static constants, and, since Java 8, static and default methods. Key differences between interfaces and abstract classes include the absence of instance variables and constructors in interfaces, and the ability to have multiple inheritance through interfaces.

Uploaded by

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

Interfaces in Java — OOP

What is an Interface?

A class is a blueprint for making objects. An interface is a blueprint for making a class. In
other words, an interface defines what a class should do, without providing the
implementation details.

Why Interfaces? — The Multiple Inheritance Problem

Java does not support multiple inheritance with classes. For example, if you have three
classes — MusicPlayer, Camera, and Phone — a Smartphone class cannot extend all three at
the same time. Java simply does not allow it. Interfaces solve this problem. A class can
implement multiple interfaces, which effectively achieves multiple inheritance.

What does an Interface contain?

By default (before Java 8), an interface could only have:

1. Abstract methods — method declarations with no body (no curly braces). You do not
need to write public or abstract explicitly because the compiler treats all interface
methods as public and abstract automatically. Writing them is redundant.
2. Static constants — variables are automatically public static final in an interface.
Again, writing these keywords explicitly is redundant.

Implementing an Interface

 A class uses the implements keyword instead of extends to work with an interface.
 The implementing class must provide a body (implementation) for all abstract
methods in the interface.
 If it does not, the class itself must be declared abstract.
 You cannot create an object of an interface (same reason as abstract classes — there is
no implementation).
 Constants defined in an interface are inherited by all implementing classes, so you can
access them through the implementing class or directly through the interface name.

Two main uses of Interfaces

1. Achieving multiple inheritance — one class can implement multiple interfaces


simultaneously with no error.
2. Achieving pure abstraction — unlike abstract classes, interfaces (originally) have no
concrete methods at all.

Interface vs Abstract Class — Key Differences

Abstract Class Interface


Instance variables Yes No
Constructor Yes No
Abstract Class Interface
Concrete (regular) methods Yes No (traditionally)
Multiple inheritance No (extend only one) Yes (implement many)

Java 8 additions — Static and Default Methods

Java 8 introduced two new types of methods allowed inside interfaces:

1. Static Methods

 You can write a static method with a body inside an interface.


 It is called using the interface name directly, e.g., [Link]().
 It cannot be called through the implementing class (Dog, Cat, etc.) — the compiler
restricts this for clarity.
 Use case: useful to provide utility/helper functionality directly associated with the
interface, similar to a utility class.

2. Default Methods

 Written with the default keyword followed by a method body.


 This is a concrete (non-abstract) method inside an interface.
 Implementing classes inherit this method automatically — they are not forced to
override it.
 Key advantage: if you add a new default method to an interface that already has 100
implementing classes, none of those classes break. They all inherit the default
implementation silently. If you added a regular abstract method instead, all 100
classes would get compilation errors.
 Default methods can use this, which at runtime refers to the instance of the
implementing class that called the method.

Can you write a main method inside an interface?

Yes. Since main is static, and static methods are allowed in interfaces (from Java 8), you can
write and run a main method inside an interface without any error.

Summary — What can an Interface contain?

 Abstract methods (implicitly public and abstract)


 Static constants (implicitly public static final)
 Static methods with bodies (Java 8+)
 Default methods with bodies (Java 8+)
1. Basic Interface — Abstract Methods

java
interface Animal {
void eat(); // implicitly public and abstract
void sleep();
}

class Dog implements Animal {


@Override
public void eat() {
[Link]("Dog is eating");
}

@Override
public void sleep() {
[Link]("Dog is sleeping");
}
}

class Cat implements Animal {


@Override
public void eat() {
[Link]("Cat is eating");
}

@Override
public void sleep() {
[Link]("Cat is sleeping");
}
}

class Test {
public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // Dog is eating
[Link](); // Dog is sleeping

Cat cat = new Cat();


[Link](); // Cat is eating
}
}

2. Static Constant in Interface

java
interface Animal {
int MAX_AGE = 150; // implicitly public static final
void eat();
}

class Dog implements Animal {


@Override
public void eat() {
[Link]("Dog is eating");
}
}
class Test {
public static void main(String[] args) {
// Access constant via interface name (preferred)
[Link](Animal.MAX_AGE); // 150

// Also accessible via implementing class (but gives a warning)


[Link](Dog.MAX_AGE); // 150
}
}

3. Cannot Instantiate an Interface

java
interface Animal {
void eat();
}

class Test {
public static void main(String[] args) {
// Animal a = new Animal(); // COMPILE ERROR — cannot instantiate interface
Animal a = new Dog(); // This is fine — upcasting
[Link]();
}
}

4. Static Method in Interface (Java 8+)

java
interface Animal {
void eat();

// static method with body — call using interface name only


static void info() {
[Link]("This is the Animal interface");
}
}

class Dog implements Animal {


@Override
public void eat() {
[Link]("Dog is eating");
}
}

class Test {
public static void main(String[] args) {
[Link](); // This is the Animal interface

// [Link](); // COMPILE ERROR — static interface methods


// can only be called via the interface name
}
}
5. Default Method in Interface (Java 8+)

java
interface Animal {
void eat();

// default method — has a body, not forced on implementing classes


default void run() {
[Link]("Animal is running");
}
}

class Dog implements Animal {


@Override
public void eat() {
[Link]("Dog is eating");
}
// run() is NOT overridden — Dog inherits the default version
}

class Cat implements Animal {


@Override
public void eat() {
[Link]("Cat is eating");
}

@Override
public void run() {
// Cat overrides run() with its own version
[Link]("Cat is running fast");
}
}

class Test {
public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // Animal is running (uses default)

Cat cat = new Cat();


[Link](); // Cat is running fast (uses overridden version)
}
}

The key point here: if you had added run() as an abstract method instead of a default
method, both Dog and Cat would have immediately got a compile error asking you to
implement it. With default, no disruption.

6. Multiple Inheritance via Interfaces

java
interface MusicPlayer {
void playMusic();
void stopMusic();
}
interface Phone {
void makeCall(String number);
void endCall();
}

interface Camera {
void takePhoto();
void recordVideo();
}

// Smartphone implements all three — multiple inheritance achieved


class Smartphone implements MusicPlayer, Phone, Camera {

@Override
public void playMusic() {
[Link]("Playing music");
}

@Override
public void stopMusic() {
[Link]("Music stopped");
}

@Override
public void makeCall(String number) {
[Link]("Calling: " + number);
}

@Override
public void endCall() {
[Link]("Call ended");
}

@Override
public void takePhoto() {
[Link]("Photo taken");
}

@Override
public void recordVideo() {
[Link]("Recording video");
}
}

class Test {
public static void main(String[] args) {
Smartphone s = new Smartphone();
[Link]("0300-1234567");
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

7. Main Method Inside an Interface


java
interface ABC {
static void info() {
[Link]("Static method in interface");
}

// main method is allowed because it is static


static void main(String[] args) {
[Link]("Hello from interface main");
[Link]();
}
}

You can run this directly: javac [Link] then java ABC.

8. Everything Together — Full Summary Example

java
interface Vehicle {
int MAX_SPEED = 200; // static constant

void accelerate(); // abstract method


void applyBrake(); // abstract method

static void info() { // static method (Java 8+)


[Link]("Vehicle Interface — max speed: " + MAX_SPEED);
}

default void fuelStatus() { // default method (Java 8+)


[Link]("Fuel status: OK");
}
}

class Car implements Vehicle {


@Override
public void accelerate() {
[Link]("Car accelerating");
}

@Override
public void applyBrake() {
[Link]("Car braking");
}
}

class Bicycle implements Vehicle {


@Override
public void accelerate() {
[Link]("Bicycle speeding up");
}

@Override
public void applyBrake() {
[Link]("Bicycle braking");
}
@Override
public void fuelStatus() {
[Link]("No fuel needed — it's a bicycle");
}
}

class Test {
public static void main(String[] args) {
[Link](); // static method via interface name

Car car = new Car();


[Link]();
[Link]();
[Link](); // inherits default version

Bicycle bike = new Bicycle();


[Link]();
[Link](); // uses its own overridden version

[Link]("Max speed: " + Vehicle.MAX_SPEED);


}
}

You might also like