Extending an Abstract Class Flashcards
Give an example of a concrete class that extends an abstract
class that extends another abstract class !
public abstract class Animal {
public abstract String getName();
}
public abstract class BigCat extends Animal {
public abstract void roar();
}
public class Lion extends BigCat {
public String getName() {
return “Lion”;
}
public void roar() {
System.out.println(“The Lion lets out a loud ROAR!”);
}
}
BigCat extends Animal but is marked as abstract; therefore, it is not required to provide an implementation for the getName() method.
The class Lion is not marked as abstract, and as the first concrete subclass, it must implement all inherited
abstract methods defined in a parent class.
When is a concrete subclass not required to provide an implementation for an abstract method of an abstract super class ?
a concrete class that extends an abstract super class can choose to not override an abstract method declared in one of the abstract superclasses if an intermediate abstract class has given the implementation
EXAMPLE :
public abstract class Animal {
public abstract String getName();
}
public abstract class BigCat extends Animal {
public String getName() {
return “BigCat”;
}
public abstract void roar();
}
public class Lion extends BigCat {
public void roar() {
System.out.println(“The Lion lets out a loud ROAR!”);
}
}
Give the rules for abstract class definition !
- Abstract classes cannot be instantiated directly.
- Abstract classes may be defined with any number, including zero, of abstract and nonabstract methods.
- Abstract classes may not be marked as private or final.
- An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
- The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.
Give the rules for abstract method definition !
- Abstract methods may only be defined in abstract classes.
- Abstract methods may not be declared private or final.
- Abstract methods must not provide a method body/implementation in the abstract class for which is it declared.
- Implementing an abstract method in a subclass follows the same rules for overriding a method. For example, the name and signature must be the same, and the visibility of the method in the subclass must be at least as accessible as the method in the parent class.