Chapter 7: Inheritance Flashcards
Why do we use inheritance? How do we use it?
1. Reduce code repetition and increase code reusability Animal common attributes and methods will not need to be written again in child class such as dog, cat, fish, bird
- Generalization
- use the ‘extends’ keyword: class Chicken extends Animal
What happens when you new a child class object?
If the child constructor is not defined, the compiler will automatically assign an empty constructor for you.
In every child constructor, there is an invisible super() which calls the parent constructor.
If needed, you can manually include the super constructor.
If we have class Animal {private name;}, and class Chicken extends Animal {private color;}, can we define a constructor like this:
public Chicken(String name, String color) { this. name = name; this.color = color; }
This is illegal as the ‘name’ attribute is private so it can only be used within the Animal class definition.
The solution can be calling a public setter method of the Animal class.
What is Overriding? What cannot be overridden? Anything you need to be cautious about overriding?
Overriding happens when a method has been defined in the parent class but the child class is not happy about it and wants to rewrite it.
Anything that is decorated by ‘final’ cannot be overridden.
- you cannot change any primitive return type. Reference type can be changed can only be changed to the child class of the parent return type. (make sure the existing methods used will not be affected and can still work properly)
- Changing access permission: parent method private can be changed to public but public parent method cannot be changed to private
How to use super() and how to use this()?
These are the two constructors. In the child constructor definition, we can use super() to help initialize some attributes defined in the parent class.
the same thing can be done using super.method();
this() calls for other constructors defined in the child class, which can also help reduce repetition.
Explain: ‘Is a’ vs ‘has a’ relationship
‘Is a’ relationship is about an object being a type. A single object can have many types. chicken is a bird and is also an animal. (But can only extends once!)
‘Has a’ relationship is about aggregation or composition.
overriding the equals() method:
Chicken.equals(Chicken otherChicken )
is this the correct way to override the equals method? if not how to override equals correctly?
This is not overriding but overloading since the method signatures are not the same.
the original equals method in the Object class: object.equals(Object otherObject)
so to correctly override: Chicken.equals(Object otherObject) { // check Object type first: with getClass() but not instanceof (why?) // compare the values }
Is this correct:
class Driver { public static void main(String[] args) { Chicken chick = new Chicken(); chick.super.yell(); } }
This is not legal!
the ‘super’ and the ‘this’ keyword cannot be used outside of the class definition.