Chapter 5 - Class Design Flashcards
What are the rules for a concrete class to extend an abstract class?
- The concrete class should implement all the methods from the abstract class.
Abstract classes extending abstract classes do not need to implemet the methods from the class that is being extended.
Can an abstract class be instantiated directly?
False
Do abstract classes require abstract and no abstract methods to be defined?
No. Abstract classes may define any number or zero abstract or non-abstract methods.
Can abstract classes may be marked as private, protected or final?
No. By any means abstract classes can be marked as private, protected or final.
Does an abstract class that extends an abstract class inherits all of its abstract methods as is own abstract methods?
True
Does the first concrete class that extends an abstract class must provide an implementation for all the inherited abstract methods?
True
Must abstract methods only be defined in abstract classes?
Yes. Abstract methods cannot be defined in classes that are not abstract.
Can abstract methods be declared private or final?
No. Abstract methods cannot be of the type private or final.
What are the rules for implementing an abstract method?
- The name and signature of the implemented method should be the same.
- The visibility of the method in the subclass must be at least as accessible as the method in the parent class.
What an interface is?
An interface is a specialized kind of abstract class. It shares many of the same properties and rules as an abstract class.
Can an interface be intanciated directly?
No. This is the interfaces rule # 1. Interfaces as abstract classes cannot be directly instantiated.
Must interfaces define methods?
No. An interface is not required to define methods.
Can an interface be defined as final?
No. This is interfaces rule #3. An interface may not be marked as final.
Must interfaces have the abstract keyword in them?
No. Top level interfaces are asume to be abstract whether the “abstract” keyword id present or not.
What will happen if an interface method is marked as private, protected or final?
A compiler error will be emitted. All top level interfaces are assume to be abstract whether the keyword is present or not. Therefore it is not possible to make a method private, protected or final.
Can a nondefault method in an interface be marked as private, protected or final?
No. This will trigger a compiler error. All nondefault methods in an interface are assume to have the modifiers abstract and public in their definition. So, defining methods as private, protected or final should not be done.
What is the inheriting an interface rule # 1?
An interface that extends another interface, as well as an abstract class that implements an interface, inherits all the abstract methods as its own abstract methods.
What is the inheriting an interface rule # 2?
The first concrete class that implements an interface, or extends an abstract class that implements an interface, must provide an implementation for all the inherited abstract methods.
Can an abstract class extend several abstract classes at the same time?
No. Just one abstract class can be extended at a time.
Can multiple interfaces be extended at the same time?
Yes. An interface may extend multiple interfaces.
Example:
public interface Seal extends HasTail, HasWhiskers{ }
Where HasTail and HasWiskers are interfaces.
Do abstract classes must include ‘abstract’ keyword in their definition?
Yes, abstract classes must include abstract keyword.