5 Inheritance Flashcards
What do subclasses include from the parent class?
new child subclass automatically includes any public or protected primitives, objects, or methods defined in the parent class.
If a class is marked final, what does that mean?
The class cannot be subclassed
Explain how super and this work for constructors in a subclass and where they are located.
In Java, the first statement of every constructor is either a call to another constructor within the class, using this(), or a call to a constructor in the direct parent class, using super(). If a parent constructor takes arguments, the super constructor would also take arguments. For simplicity in this section, we refer to the super() command as any parent constructor, even those that take an argument. Notice the user of both super() and super(age) in the following example: public class Animal { private int age; public Animal(int age) { super(); this.age = age; } } public class Zebra extends Animal { public Zebra(int age) { super(age); } public Zebra() { this(4); } }
What are the constructor rules?
Constructor Defi nition Rules: 1. The first statement of every constructor is a call to another constructor within the class using this(), or a call to a constructor in the direct parent class using super(). 2. The super() call may not be used after the first statement of the constructor. 3. If no super() call is declared in a constructor, Java will insert a no-argument super() as the first statement of the constructor. 4. If the parent doesn’t have a no-argument constructor and the child doesn’t define any constructors, the compiler will throw an error and try to insert a default no-argument constructor into the child class. 5. If the parent doesn’t have a no-argument constructor, the compiler requires an explicit call to a parent constructor in each child constructor. Make sure you understand these rules; the exam will often provide code that breaks one or many of these rules and therefore doesn’t compile.
What is a good trick question about inheirtance?
You should be wary of any exam question in which the parent class defines a constructor that takes arguments and doesn’t define a no-argument constructor. Be sure to check that the code compiles before answering a question about it.
Describe the rules for this and super when used with parent and subclass members.
x
What does the compiler check when overriding a method?
Compiler checks:
- The method in the child class must have the same signature as the method in the parent class.
- The method in the child class must be at least as accessible or more accessible than the
method in the parent class. - The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method.
- If the method returns a value, it must be the same or a subclass of the method in the parent class, known as covariant return types.
Explain covariance
x
Can the child method have an exception if the parent method does not?
No, it can’t. Code will not compile.
Can the child method have an exception if the parent method does not?
No, it can’t. (Except runtime exceptions) Code will not compile. Breaks rules for overriding methods.
Can private methods be overridden in the subclass?
No. Private means no other classes have access. Just redeclare the method in the subclass and design it however you want.
What are the five rules for hiding a method?
1. The method in the child class must have the same signature as the method in the parent class. 2. The method in the child class must be at least as accessible or more accessible than the method in the parent class. 3. The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method. 4. If the method returns a value, it must be the same or a subclass of the method in the parent class, known as covariant return types. 5. The method defined in the child class must be marked as static if it is marked as static in the parent class (method hiding). Likewise, the method must not be marked as static in the child class if it is not marked as static in the parent class (method overriding). Note that the fi rst four are the same as the rules for overriding a method.
What are the five rules for hiding a method?
1. The method in the child class must have the same signature as the method in the parent class. 2. The method in the child class must be at least as accessible or more accessible than the method in the parent class. 3. The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method. 4. If the method returns a value, it must be the same or a subclass of the method in the parent class, known as covariant return types. 5. The method defined in the child class must be marked as static if it is marked as static in the parent class (method hiding). Likewise, the method must not be marked as static in the child class if it is not marked as static in the parent class (method overriding). Note that the first four are the same as the rules for overriding a method.
Can a final method be overridden?
No.
Can a static final method be overridden/hidden?
No, final cannot be subclassed
Can a class be abstract?
yes.