Chapter 7: Inheritance Flashcards

1
Q

Why do we use inheritance? How do we use it?

A
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
  1. Generalization
  2. use the ‘extends’ keyword: class Chicken extends Animal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens when you new a child class object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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;
}
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Overriding? What cannot be overridden? Anything you need to be cautious about overriding?

A

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.

  1. 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)
  2. Changing access permission: parent method private can be changed to public but public parent method cannot be changed to private
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to use super() and how to use this()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain: ‘Is a’ vs ‘has a’ relationship

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A

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
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is this correct:

class Driver {
    public static void main(String[] args) {
        Chicken chick = new Chicken();
        chick.super.yell();
    }
}
A

This is not legal!

the ‘super’ and the ‘this’ keyword cannot be used outside of the class definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly