Abstract Classes and Interfaces
Abstract Classes and Interfaces
● No creating instances of abstract classes with new but they still have
constructors (even with parameters)
○ Note in java if a superclass ONLY has a constructor that takes arguments
(meaning no default constructor is made), the subclass must call
Super(arguments) in the subclass constructor (even if the subclass
constructor takes no arguments). But if both a default constructor and a
constructor that takes arguments OR there is no constructor declared (in
which case Java makes a default constructor) then there is no need to
explicitly type Super(...) since Java will automatically do it.
● Not that ONLY abstract methods can have no bodies. Any other method must
have a body.
● Abstract methods are not static
● A subclass method can override a superclass method and make it abstract
● You cannot cast siblings classes
● This throws an error because Number does not implement
Comparable<Number>”:
Interfaces
● A Java class can extend only one superclass but can implement multiple
interfaces
Interface Animal {
void makeSound();
}
…..main {
● You can cast interface objects and classes that implement it the same way as
other classes: