0
0
Javaprogramming~5 mins

Default methods in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a default method in Java interfaces?
A default method is a method defined in an interface with a body. It allows interfaces to have method implementations without breaking existing classes that implement the interface.
Click to reveal answer
beginner
How do you declare a default method in a Java interface?
Use the default keyword before the method name in the interface. For example:<br>
default void sayHello() { System.out.println("Hello"); }
Click to reveal answer
intermediate
Why were default methods introduced in Java 8?
They were introduced to allow adding new methods to interfaces without breaking existing implementations. This helps evolve interfaces over time.
Click to reveal answer
beginner
Can a class override a default method from an interface?
Yes, a class that implements the interface can provide its own implementation of the default method, overriding the interface's version.
Click to reveal answer
intermediate
What happens if two interfaces provide default methods with the same signature and a class implements both?
The class must override the conflicting default method to resolve the ambiguity, otherwise the code will not compile.
Click to reveal answer
Which keyword is used to declare a default method in a Java interface?
Astatic
Babstract
Cfinal
Ddefault
What is the main benefit of default methods in interfaces?
AAllow interfaces to have method implementations without breaking existing classes
BMake all interface methods static
CForce all implementing classes to override methods
DPrevent interfaces from having any methods
If a class implements two interfaces with the same default method signature, what must it do?
AOverride the method to resolve the conflict
BUse the method from the first interface only
CUse the method from the second interface only
DNothing, it compiles fine without changes
Can default methods call other methods inside the interface?
AOnly static methods can be called
BNo, default methods cannot call any methods
CYes, they can call other interface methods
DOnly methods from Object class can be called
Which Java version introduced default methods?
AJava 7
BJava 8
CJava 6
DJava 9
Explain what default methods are and why they are useful in Java interfaces.
Think about how interfaces evolved to add new features without breaking old code.
You got /4 concepts.
    Describe how Java handles conflicts when a class implements two interfaces with the same default method.
    Consider what happens when two parents give the same instruction.
    You got /4 concepts.