Class Design Flashcards
What is included when a class inherits from another?
The public and protected primitives, objects, or methods defined in the parent class
Single Inheritance
Java only allows a class to have one direct parent class
How can you keep a class from being extended?
Mark it with the final modifier
What is the basic Object class?
java.lang.Object
How do you call the parent class’s constructor?
super()
What type of constructor must a parent class have if you don’t specify a certain constructor in the child class to make things work?
A default no-arguments constructor
What are the rules for overriding a method in a child class?
1) The method in the child class must have the same exact signature (name and parameters) as the one in the parent class
2) The method in the child class must be at least as accessible as the one in the parent class
3) The method in the child class may not throw a checked exception that is new or broader than in the parent method
4) Return type of the child class method must be the same or a subclass of the return type in the parent class
Hidden Method
When a child class defines a static method with the same name and signature as a static method defined in the parent class.
Can you override or hide variables?
You can only hide them
Abstract Class
A class that is marked with the abstract keyword and cannot be instantiated.
Abstract Method
A method marked with the abstract keyword defined in an abstract class, for which no implementation is provided in the class in which it is declared. Can only be declared in an abstract class.
public abstract String() getName();
Concrete Class
The first non-abstract subclass that extends an abstract class. It is required to implement all inherited abstract methods, which have not been implemented in a parent class.
Can an abstract class that extends an abstract class provide an implementation for methods inherited?
yes! but they don’t have to
Abstract Class Definition Rules
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.
Abstract Method Definition Rules
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.