class A { protected String foo() throws Exception{ return "a"; }}class B extends A { // protected --> public (more access) // Exception --> IOException (less broad) // throws clause can be omitted completely @Override public String foo() throws IOException { return "b"; }}
Multiple Inheritance with interfaces
Multiple inheritance is achieved by default interface methods since they are inherited and provide implementation
static methods in interfaces are never inherited
Hence you should call them by their parent interface name directly
Inheritance rules:
Instance methods are preferred over interface default methods
// Horse, Flyer and Mythical all have implementation for identifyMyself()// Horse method will be preferred (no overriding)class Pegasus extends Horse implements Flyer, Mythical {}
Methods that are already overridden by other candidates are ignored. This circumstance can arise when super types share a common ancestor.
// has default method: identifyMyself()interface Animal {}// has default method: identifyMyself()interface EggLayer extends Animal {}// does not define any method, takes identifyMyself() from Animalinterface FireBreather extends Animal {}// will take EggLayer implementationclass Dragon implements EggLayer, FireBreather {}
If two or more independently defined default methods conflict, or a default method conflicts with an abstract method, then the Java compiler produces a compiler error
public interface OperateCar { default public int startEngine(EncryptedKey key) { /*implementation*/ }}public interface FlyCar { default public int startEngine(EncryptedKey key) { /*implementation*/ }}// must define startEngine() since there is a clashpublic class FlyingCar implements OperateCar, FlyCar { }