Chapter 9 Flashcards
What is an abstract class
An abstract class is a class that cannot be instantiated and may contain abstract methods.
What is an abstract method
An abstract method is a method that does not define an implementation when it is declared.
True or false
An abstract class must have at least one abstract method.
False
True or false
An abstract method can only be defined in an abstract class.
True
True or false
An abstract class can include constructors
True
True or false
The final and abstract modifiers can be placed before or after the access modifier in classes and methods.
True
Why can an abstract class have a constructor?
Abstract classes are initialized with constructors the same way non-abstract classes are. The difference is that it can only be called through a super() method from a subclass.
Why does Java not permit a class or method be abstract and final?
With abstract, you intend for someone else to extend or implement it. With final you are preventing anyone from extending or implementing it.
These are in direct conflict with eachother.
Why does Java not permit a class or method be abstract and private?
With abstract, you intend for someone else to extend or implement it. with private, we restrict access to the class itself.
These are in direct conflict with eachother.
Why does Java not permit a method be abstract and static?
With abstract, you intend for someone else to extend or implement it. With static, it belongs to the class, not an instance of a class and thus cannot be overridden.
These are in direct conflict with eachother.
What are the abstract class definition rules?
- Abstract classes cannot be instantiated
- All top-level types, including abstract classes, cannot be marked protected or private
- Abstract classes cannot be marked final
- Abstract classes may include zero or more abstract and nonabstract methods.
- An abstract class that extends another abstract class inherits all of its abstract methods.
- The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.
- Abstract class constructors follow the same rules for initialization as regular constructors, except they can be called only as part of the initialization of a subclass.
What are the abstract method definition rules?
- Abstract methods may be defined only in abstract classes or interfaces
- Abstract methods cannot be declared private, static or final
- Abstract methods must not provide a method body/implementation in the abstract class in which they are declared.
- Implementing an abstract method in a subclass follows the same rules for overriding a method, including covariant return types, exception declarations, etc.
Which access modifier is implicitely (automatically inserted by the compiler) used when declaring abstract methods or constant variables in an interface?
public.
What are the implicit (automatically inserted by the compiler) modifiers of interface variables?
public static final
all variables inside an interface are constant variables
What are the implicit (automatically inserted by the compiler) modifiers of interface methods?
public abstract