Extending an Abstract Class Flashcards

1
Q

Give an example of a concrete class that extends an abstract
class that extends another abstract class !

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is a concrete subclass not required to provide an implementation for an abstract method of an abstract super class ?

A

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!”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give the rules for abstract class definition !

A
  1. Abstract classes cannot be instantiated directly.
  2. Abstract classes may be defined with any number, including zero, of abstract and nonabstract methods.
  3. Abstract classes may not be marked as private or final.
  4. An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
  5. The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give the rules for abstract method definition !

A
  1. Abstract methods may only be defined in abstract classes.
  2. Abstract methods may not be declared private or final.
  3. Abstract methods must not provide a method body/implementation in the abstract class for which is it declared.
  4. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly