Introduction
Default Method in Java 8 is used for adding new methods in Interface without needing to altering implementing Classes.
Example: Original version
public interface Car {
public int getSpeed();
}
public class SportCar implements Car {
@Override
public int getSpeed() {
return 100;
}
}
Now we want to add new methods to interface, but if we did we also need to update implementing Classes. Instead of doing that we could use the new Default Method feature in Java 8
public interface Car {
public int getSpeed();
public default Color getColor() {
return Color.RED;
}
}
No comments:
Post a Comment