0% found this document useful (0 votes)
1 views

Interfaces in java and how they are different with inheritance

An interface in Java serves as a blueprint for classes, requiring them to implement specified methods without providing the actual code. Key features include support for default and static methods starting from Java 8, the inability to have constructors, and the ability to implement multiple interfaces for achieving multiple inheritance. Interfaces are primarily used to define capabilities or roles that various classes can adopt.

Uploaded by

k246145
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)
1 views

Interfaces in java and how they are different with inheritance

An interface in Java serves as a blueprint for classes, requiring them to implement specified methods without providing the actual code. Key features include support for default and static methods starting from Java 8, the inability to have constructors, and the ability to implement multiple interfaces for achieving multiple inheritance. Interfaces are primarily used to define capabilities or roles that various classes can adopt.

Uploaded by

k246145
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/ 4

Interfaces in Java:

(Things To Know)

What is an Interface in Java?


An interface in Java is like a contract or a blueprint for classes. It tells the class, “Yo, if you
implement me, you must provide the behavior I describe.”

It’s kinda like a to-do list for a class. No actual code — just the method names and their
parameters. You implement the details later.

interface Animal {
void makeSound(); // method with no body
}

Now any class that implements Animal has to define makeSound().

class Dog implements Animal {


public void makeSound() {
System.out.println("Bark!");
}
}

Key Features of Interfaces in Java


Let’s break it down feature by feature:

1. Method Declarations Only (before Java 8)

Interfaces used to only allow abstract methods — method signatures with no body. Like:

void run();

But things got spicier with Java 8+...

2. Default Methods (Java 8+)

Now you can have method bodies using the default keyword.

default void greet() {


System.out.println("Hello from interface!");
}
Classes can override these if they want.

3. Static Methods (Java 8+)

Interfaces can also have static methods.

static void printInfo() {


System.out.println("I'm a static method in an interface.");
}

Called like this:

Animal.printInfo();

4. No Constructors

Interfaces can’t have constructors. Why? Because you can't create objects of an interface
directly. They’re not real-world things — they’re more like guidelines.

5. Multiple Inheritance (sorta)

A class in Java can’t extend more than one class. But it can implement multiple interfaces.
That’s Java's way of doing multiple inheritance:

interface A { void doA(); }


interface B { void doB(); }

class C implements A, B {
public void doA() { System.out.println("A"); }
public void doB() { System.out.println("B"); }
}

6. All methods are public abstract by default

Even if you don’t write public abstract, it’s understood:

void jump(); // is same as:


public abstract void jump();
7. Fields are public static final by default

Interfaces can have variables, but they are constants.

int x = 10; // same as public static final int x = 10;

Which means:

 You can’t change x.


 It belongs to the interface, not the object.

8. Functional Interfaces (Java 8+)

If an interface has exactly one abstract method, it's called a functional interface — used with
lambda expressions.

@FunctionalInterface
interface Greeting {
void sayHello();
}

Greeting g = () -> System.out.println("Hey there!");


g.sayHello();

TL;DR Cheat Sheet


Feature Interface Support
Abstract Methods ✅
Default Methods (Java 8+) ✅
Static Methods (Java 8+) ✅
Constructors ❌
Variables Only public static final
Multiple Inheritance ✅ (via interfaces)
Functional Interfaces ✅ (with one method)
Inheritance vs Interface in Java — The Ultimate Face-
Off
Feature Inheritance (Class Extends) Interface (Implements)
Keyword Used extends implements
Type Class-to-Class Class-to-Interface
❌ Not allowed (only one class ✅ Allowed (can implement
Multiple Inheritance
can be extended) multiple interfaces)
Method Only abstract methods (unless using
Can have full method definitions
Implementation default/static)
Constructor ✅ Subclass inherits constructor
❌ No constructors in interfaces
Inheritance indirectly
Access Modifiers on Can be public, protected, or Methods are public by default
Methods private
Can inherit instance and static Only public static final
Field Inheritance
variables (constants)
For "is-a" relationship and shared For "can-do" capability or common
Use Case
implementation behavior
default, static, and private
Java 8+ Features Normal class methods
methods allowed
Parent class can be abstract or All methods are abstract unless
Abstract Requirement
concrete specified
Polymorphism
✅ Yes ✅ Yes
Support

When to Use What?

 Use inheritance when you want to share actual code and create a strong "is-a"
relationship.
➤ Example: Car extends Vehicle
 Use interface when you want to define a capability or role that multiple classes can play.
➤ Example: Bird implements Flyable

You might also like