Introduction:
Java 8 introduces 'default' method declaration in interfaces. The idea behind this to make a change in the interface (adding new method signature) without affecting already implemented classes of the interface. This a nice thing to kept the old implementations intact still adding new features whoever needs it. See Example-01.
Notes:
1. Extend an interface with a default method
Java 8 introduces 'default' method declaration in interfaces. The idea behind this to make a change in the interface (adding new method signature) without affecting already implemented classes of the interface. This a nice thing to kept the old implementations intact still adding new features whoever needs it. See Example-01.
Notes:
1. Extend an interface with a default method
- Not mention the default method at all, which lets your extended interface inherit the default method
- Re-declare the default method, which makes it abstract
- Redefine the default method, which overrides it
2. Conflict with multiple interface - if a class uses multiple interfaces having same default method signature, the implementation class should explicitly specify which default method to use or define it's own one. Because the implementation class does not know which default method to use. See Example-02.
Examples:
Example-01: Bird class inherits FlyInterface where the interface has a default implementation which Bird class is happy with rather than providing it's own implementation.
BehaviorInterface -
public interface BehaviorInterface {
public default void fly(){
System.out.println("Generic fly");
}
public default void sing(){
System.out.println("Generic sing");
}
public void swim();
}
Bird:
public class Bird implements BehaviorInterface{
public Bird(){
System.out.println("A bird is created");
}
@Override
public void swim(){
System.out.println("Swim by me");
}
@Override
public void sing(){
System.out.println("My own singing");
}
}
Test above implementation:
public class TestBird {
public static void main(String[] args){
Bird aBird = new Bird();
aBird.fly();
aBird.sing();
aBird.swim();
}
}
Output:
A bird is created
Generic fly
My own singing
Swim by me
Example-02: Two interface with same default method would throw compile time error 'Duplicate default methods named speed with the parameters () and () are inherited from the types SwimInterface and FlyInterface'.
FlyInterface -
public interface FlyInterface {
public void fly();
public default void print(){
System.out.println("Print in FlyInterface");
}
}
SwimInterface -
public interface SwimInterface {
public void swim();
public default void print(){
System.out.println("Print in SwimInterface");
}
}
NewBird -
public class NewBird implements FlyInterface, SwimInterface {
public NewBird(){
System.out.println("A new bird is created");
}
@Override
public void swim() {
System.out.println("My swim implementation");
}
@Override
public void fly() {
System.out.println("My fly implementation");
}
@Override
public void print(){
System.out.println("I am forced to provide specific implementation");
FlyInterface.super.print();
SwimInterface.super.print();
}
}
Test above implementation -
public class TestNewBird {
public static void main(String[] args){
NewBird aBird = new NewBird();
aBird.fly();
aBird.swim();
aBird.print();
}
}
Output:
A new bird is created
My fly implementation
My swim implementation
I am forced to provide specific implementation
Print in FlyInterface
Print in SwimInterface
Resources:
1. https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
2. http://www.programcreek.com/2014/12/default-methods-in-java-8-and-multiple-inheritance/
Example-01: Bird class inherits FlyInterface where the interface has a default implementation which Bird class is happy with rather than providing it's own implementation.
BehaviorInterface -
public interface BehaviorInterface {
public default void fly(){
System.out.println("Generic fly");
}
public default void sing(){
System.out.println("Generic sing");
}
public void swim();
}
public class Bird implements BehaviorInterface{
public Bird(){
System.out.println("A bird is created");
}
@Override
public void swim(){
System.out.println("Swim by me");
}
@Override
public void sing(){
System.out.println("My own singing");
}
}
Test above implementation:
public class TestBird {
public static void main(String[] args){
Bird aBird = new Bird();
aBird.fly();
aBird.sing();
aBird.swim();
}
}
Output:
A bird is created
Generic fly
My own singing
Swim by me
Example-02: Two interface with same default method would throw compile time error 'Duplicate default methods named speed with the parameters () and () are inherited from the types SwimInterface and FlyInterface'.
FlyInterface -
public interface FlyInterface {
public void fly();
public default void print(){
System.out.println("Print in FlyInterface");
}
}
SwimInterface -
public interface SwimInterface {
public void swim();
public default void print(){
System.out.println("Print in SwimInterface");
}
}
NewBird -
public class NewBird implements FlyInterface, SwimInterface {
public NewBird(){
System.out.println("A new bird is created");
}
@Override
public void swim() {
System.out.println("My swim implementation");
}
@Override
public void fly() {
System.out.println("My fly implementation");
}
@Override
public void print(){
System.out.println("I am forced to provide specific implementation");
FlyInterface.super.print();
SwimInterface.super.print();
}
}
Test above implementation -
public class TestNewBird {
public static void main(String[] args){
NewBird aBird = new NewBird();
aBird.fly();
aBird.swim();
aBird.print();
}
}
Output:
A new bird is created
My fly implementation
My swim implementation
I am forced to provide specific implementation
Print in FlyInterface
Print in SwimInterface
Resources:
1. https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
2. http://www.programcreek.com/2014/12/default-methods-in-java-8-and-multiple-inheritance/
No comments:
Post a Comment