0% found this document useful (0 votes)
5 views6 pages

Default and Static Method

Java 8 introduced default and static methods in interfaces to enhance functionality without breaking existing code. Default methods allow interfaces to have methods with a body that can be overridden in implementing classes, while static methods belong to the interface itself and can only be called using the interface name. The document also discusses the 'Diamond Problem' where conflicts arise from multiple interfaces having the same default method, requiring explicit overrides in implementing classes.

Uploaded by

sandhya.sharma
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)
5 views6 pages

Default and Static Method

Java 8 introduced default and static methods in interfaces to enhance functionality without breaking existing code. Default methods allow interfaces to have methods with a body that can be overridden in implementing classes, while static methods belong to the interface itself and can only be called using the interface name. The document also discusses the 'Diamond Problem' where conflicts arise from multiple interfaces having the same default method, requiring explicit overrides in implementing classes.

Uploaded by

sandhya.sharma
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
You are on page 1/ 6

What are default and static methods in Java?

These were introduced in Java 8 to add new methods in interfaces without breaking existing
code.

🔹 1. Default Method in Interface

 Allows an interface to have a method with a body


 Can be overridden in implementing classes

✅ Example:

interface MyInterface {
void show(); // abstract method

default void greet() {


System.out.println("Hello from default method!");
}
}

class MyClass implements MyInterface {


public void show() {
System.out.println("Implementing show()");
}
}

public class DefaultMethodExample {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.show(); // Calls abstract method
obj.greet(); // Calls default method from interface
}
}

✅ Output:

Implementing show()
Hello from default method!

🔹 2. Static Method in Interface

 Belongs to the interface itself, not to objects


 Can be called using the interface name only

✅ Example:

interface MyInterface {
static void display() {
System.out.println("Hello from static method!");
}
}

public class StaticMethodExample {


public static void main(String[] args) {
MyInterface.display(); // Call static method using interface name
}
}

✅ Output:

Hello from static method!

🧠 Summary Table

Feature Default Method Static Method


Defined in? Interface Interface
Has method body? ✅ Yes ✅ Yes
Called using? Object of implementing class Interface name
Overridable? ✅ Yes (in class) ❌ No (cannot override)

An example where a Java interface contains both a default method and a static method,
along with a class that implements it.

✅ Combined Example: Default + Static Method in Interface


interface MyInterface {
// Abstract method
void show();

// Default method
default void greet() {
System.out.println("Hello from default method!");
}

// Static method
static void staticGreet() {
System.out.println("Hello from static method!");
}
}

class MyClass implements MyInterface {


public void show() {
System.out.println("Implementing show()");
}
}

public class CombinedExample {


public static void main(String[] args) {
MyClass obj = new MyClass();

obj.show(); // Calls overridden abstract method


obj.greet(); // Calls default method from interface
MyInterface.staticGreet(); // Calls static method from interface
}
}

✅ Output:
Implementing show()
Hello from default method!
Hello from static method!

🔍 Explanation:

Line What It Does


obj.show(); Calls the show() method implemented in MyClass
obj.greet(); Calls the default method defined in MyInterface
MyInterface.staticGreet(); Calls the static method using interface name

This setup is useful when:

 You want to provide optional behavior (via default) to all implementing classes
 You want utility/helper methods (via static) directly in the interface

see how multiple interfaces with default methods are handled (like in diamond problem
cases)?

multiple interfaces with default methods — including the diamond problem in Java — and
how it's handled.

💎 The “Diamond Problem” in Java (with Default Methods)


In Java 8+, if two interfaces provide a default method with the same name, and a class
implements both interfaces, the compiler will not know which one to use — unless you
override it.

✅ Example: Two interfaces with same default method

interface A {
default void show() {
System.out.println("Show from Interface A");
}
}

interface B {
default void show() {
System.out.println("Show from Interface B");
}
}

// Class implements both interfaces


class MyClass implements A, B {
// Must override show() to resolve conflict
public void show() {
System.out.println("Show from MyClass (resolving conflict)");
}
}

public class DiamondProblemExample {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.show(); // Calls overridden method in MyClass
}
}

✅ Output:
Show from MyClass (resolving conflict)

🔍 Explanation:

 Both A and B have a default void show() method.


 MyClass implements both — Java forces you to override show() to resolve the conflict.

✅ BONUS: Call specific interface’s default method (if needed)


You can even call a specific interface’s default method using:

A.super.show();

🔸 Updated MyClass with both calls:

class MyClass implements A, B {


public void show() {
A.super.show(); // Call A's default method
B.super.show(); // Call B's default method
System.out.println("Show from MyClass");
}
}

✅ Output:

Show from Interface A


Show from Interface B
Show from MyClass

 ✅ Abstract method (must be implemented)


 ✅ Default method (can be overridden or used as-is)
 ✅ Static method (called using the interface name)

All in the same interface, and show how a class uses them.

✅ Java Interface with Abstract, Default, and Static Methods


🔸 Example Code:
interface MyInterface {
// Abstract method - must be implemented
void sayHello(String name);

// Default method - optional to override


default void greet() {
System.out.println("Hello from default method!");
}

// Static method - called using interface name


static void info() {
System.out.println("This is a static method in interface.");
}
}
class MyClass implements MyInterface {
// Must implement abstract method
public void sayHello(String name) {
System.out.println("Hello, " + name);
}

// Optional: override the default method


@Override
public void greet() {
System.out.println("Custom greeting from MyClass!");
}
}

public class CombinedInterfaceExample {


public static void main(String[] args) {
MyClass obj = new MyClass();

obj.sayHello("John"); // Calls abstract method (implemented)


obj.greet(); // Calls overridden default method
MyInterface.info(); // Calls static method using interface name
}
}

✅ Output:
Hello, John
Custom greeting from MyClass!
This is a static method in interface.

🧠 Summary:

Method Type Defined In Called On Overridable?


Abstract Interface Must implement ✅ Required
Default Interface Via object ✅ Optional
Static Interface InterfaceName. ❌ Not allowed

You might also like