Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungKI half beim Finden: iOS 27 schließt mehr als 100 Sicherheitslücken(21.09.2026 um 06:00 Uhr)
Sichere ProgrammierungWhat Is Rowhammer? How Can Repeated Memory Access Flip Bits in RAM?(21.09.2026 um 07:12 Uhr)
Sichere Programmierungnpm publish Ignores .gitignore: The .npmignore Override Rule(21.09.2026 um 07:15 Uhr)
Sichere ProgrammierungAphelion Editor - A free node-based video / VFX editor(21.09.2026 um 07:21 Uhr)
Sichere ProgrammierungGovernance Attack Surface Review: OKX(21.09.2026 um 07:31 Uhr)
Sichere ProgrammierungJSM Portal Request Create Property Panel Submit(21.09.2026 um 07:34 Uhr)
Reverse Engineeringsearch instructions assembly easy (X86,RISCV,AARCH64,etc)(20.09.2026 um 15:44 Uhr)
Sichere ProgrammierungKI half beim Finden: iOS 27 schließt mehr als 100 Sicherheitslücken(21.09.2026 um 06:00 Uhr)
Sichere ProgrammierungWhat Is Rowhammer? How Can Repeated Memory Access Flip Bits in RAM?(21.09.2026 um 07:12 Uhr)
Sichere Programmierungnpm publish Ignores .gitignore: The .npmignore Override Rule(21.09.2026 um 07:15 Uhr)
Sichere ProgrammierungAphelion Editor - A free node-based video / VFX editor(21.09.2026 um 07:21 Uhr)
Sichere ProgrammierungGovernance Attack Surface Review: OKX(21.09.2026 um 07:31 Uhr)
Sichere ProgrammierungJSM Portal Request Create Property Panel Submit(21.09.2026 um 07:34 Uhr)
Reverse Engineeringsearch instructions assembly easy (X86,RISCV,AARCH64,etc)(20.09.2026 um 15:44 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Part 2 - extends vs implements, Interface Methods & Variables

Reagiere als Erste:r — dein Feedback zählt!

In the previous article, we learned what an interface is and why it acts as a contract between the client and the service provider.

Now it's time to understand one of the most frequently asked Java interview topics:

  • What is the difference between extends and implements?
  • Can a class extend multiple classes?
  • Can a class implement multiple interfaces?
  • Why are interface methods always public abstract?
  • Why are interface variables always public static final?

These concepts form the foundation of Java's multiple inheritance model and are commonly used in frameworks like Spring Boot, Hibernate, and the Java Collections Framework.

Why Do We Need extends and implements?

Java supports two kinds of inheritance:

  1. Implementation Inheritance (using classes)
  2. Contract Inheritance (using interfaces)

To distinguish between them, Java provides two keywords:

  • extends
  • implements

extends vs implements

The extends keyword is used when inheriting from a class or extending another interface.

The implements keyword is used when a class provides the implementation for an interface.

Keyword Used With Purpose
extends Class → Class, Interface → Interface Inherit existing behavior
implements Class → Interface Provide implementation for a contract

A Class Can Extend Only One Class

Java does not support multiple inheritance of classes.

Example:

class Employee {

    public void work() {
        System.out.println("Employee is working.");
    }

}

class Manager extends Employee {

}

Step-by-Step Explanation

Step 1

Employee defines the behavior.

Step 2

Manager extends Employee.

Step 3

Manager inherits the work() method.

Why Can't a Class Extend Multiple Classes?

Suppose Java allowed this:

class A {

    public void display() {
        System.out.println("Class A");
    }

}

class B {

    public void display() {
        System.out.println("Class B");
    }

}

class C extends A, B {

}

Which display() method should Java inherit?

        A
         \
          \
           C
          /
         /
        B

This is known as the Diamond Problem.

To avoid ambiguity, Java allows a class to extend only one class.

A Class Can Implement Multiple Interfaces

Unlike classes, interfaces contain contracts rather than implementations.

Therefore, there is no ambiguity.

Example:

interface PaymentService {

    void processPayment();

}

interface NotificationService {

    void sendNotification();

}

class OnlineOrderService implements PaymentService, NotificationService {

    @Override
    public void processPayment() {

        System.out.println("Payment processed.");

    }

    @Override
    public void sendNotification() {

        System.out.println("Notification sent.");

    }

}

Step-by-Step Explanation

Step 1

PaymentService defines one service.

Step 2

NotificationService defines another service.

Step 3

OnlineOrderService implements both interfaces.

Step 4

Both methods must be implemented.

A Class Can Extend One Class and Implement Multiple Interfaces

Java allows both together.

Example:

interface PaymentService {

    void processPayment();

}

interface NotificationService {

    void sendNotification();

}

class Order {

    public void displayOrder() {

        System.out.println("Displaying order.");

    }

}

class OnlineOrder extends Order
        implements PaymentService, NotificationService {

    @Override
    public void processPayment() {

        System.out.println("Payment completed.");

    }

    @Override
    public void sendNotification() {

        System.out.println("Customer notified.");

    }

}

This is a very common design in enterprise applications.

An Interface Can Extend Multiple Interfaces

Interfaces support multiple inheritance.

Example:

interface PaymentService {

    void processPayment();

}

interface NotificationService {

    void sendNotification();

}

interface OrderService
        extends PaymentService, NotificationService {

}

Here, OrderService inherits both method declarations.

Valid Syntax Combinations

Expression Valid? Explanation
class B extends A Class inherits another class
interface B extends A Interface extends interface
class C implements A Class implements interface
class C extends A implements B Extend one class and implement interfaces
interface C extends A, B Interface extends multiple interfaces
class C implements A extends B Invalid syntax

Interface Methods

Every method declared inside an interface is implicitly:

  • public
  • abstract

These declarations are equivalent:

interface PaymentService {

    void processPayment();

}
interface PaymentService {

    public abstract void processPayment();

}

The compiler automatically adds public and abstract.

Why Are Interface Methods Public?

Interfaces represent contracts.

Any implementation class, regardless of package, must be able to access and implement these methods.

Hence, they are public.

Why Are Interface Methods Abstract?

Interfaces specify what should happen.

Implementation classes decide how it happens.

Illegal Modifiers for Interface Methods

Traditional abstract interface methods cannot use the following modifiers:

  • private
  • protected
  • final
  • synchronized
  • native

For example:

interface PaymentService {

    final void processPayment();

}

Compile-Time Error

modifier final not allowed here

Note: Since Java 8, interfaces can also declare default and static methods, and since Java 9, private methods are allowed inside interfaces for code reuse. Those modern features will be covered in a later article. In this article, we're focusing on traditional abstract interface methods.

Interface Variables

Interfaces can contain variables.

However, every interface variable is implicitly:

  • public
  • static
  • final

Example:

interface ApplicationConstants {

    int MAX_USERS = 100;

}

The compiler treats it as:

public static final int MAX_USERS = 100;

Why Are Interface Variables Public Static Final?

Public

Every implementation class should access the constant.

Static

The value belongs to the interface itself.

No object is required.

Example:

System.out.println(ApplicationConstants.MAX_USERS);

Final

The value should never change.

Implementation classes cannot modify it.

Interface Variables Must Be Initialized

Incorrect:

interface ApplicationConstants {

    int MAX_USERS;

}

Compile-Time Error

= expected

Correct:

interface ApplicationConstants {

    int MAX_USERS = 100;

}

Interface Variables Cannot Be Modified

Example:

interface ApplicationConstants {

    int MAX_USERS = 100;

}

class Test implements ApplicationConstants {

    public static void main(String[] args) {

        // MAX_USERS = 200; ❌ Compile-time error

        System.out.println(MAX_USERS);

    }

}

The constant is inherited but cannot be reassigned.

Common Beginner Mistakes

Mistake 1: Trying to Extend Multiple Classes

class C extends A, B {

}

Java does not allow this.

Mistake 2: Forgetting to Implement Every Interface Method

Every abstract method must be implemented unless the class is abstract.

Mistake 3: Trying to Modify Interface Variables

MAX_USERS = 500;

This results in a compile-time error because interface variables are final.

Mistake 4: Thinking Interface Variables Are Instance Variables

They are static.

Access them using the interface name.

ApplicationConstants.MAX_USERS

Best Practices

  • Use extends only for inheritance between related classes.
  • Prefer interfaces to define behaviors and contracts.
  • Keep interface variables limited to true constants.
  • Avoid storing mutable state inside interfaces.
  • Use meaningful interface names such as PaymentService, NotificationService, and OrderRepository.

Interview Questions

1. What is the difference between extends and implements?

extends is used for inheritance, while implements is used to provide the implementation of an interface.

Why interviewers ask

To verify your understanding of Java inheritance.

2. Can a class extend multiple classes?

No.

Java prevents multiple class inheritance to avoid ambiguity.

3. Can a class implement multiple interfaces?

Yes.

A class can implement any number of interfaces.

4. Can an interface extend multiple interfaces?

Yes.

This is Java's way of supporting multiple inheritance of type.

5. Why are interface methods public abstract?

Because interfaces define publicly accessible contracts that implementing classes must provide.

6. Why are interface variables public static final?

They represent shared constants that belong to the interface and cannot be modified.

7. Why must interface variables be initialized?

Because they are final, and final variables must receive a value exactly once.

Quick Memory Trick 🧠

Remember EIM:

E → Extends → One Class

I → Implements → Many Interfaces

M → Multiple Interface Inheritance

Think EIM whenever you see Java inheritance.

Key Takeaways

  • extends is used for class inheritance and interface inheritance.
  • implements is used when a class implements an interface.
  • A class can extend only one class.
  • A class can implement multiple interfaces.
  • An interface can extend multiple interfaces.
  • Interface methods are implicitly public abstract.
  • Interface variables are implicitly public static final.
  • Interface constants must be initialized when declared.

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Part 2 - extends vs implements, Interface Methods & Variables

Thematisch verwandte Begriffe: Part, extends, implements, Interface · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94109 | openEQUELLA versions before 2026.1.0 contain a remote code execution vul…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick