Unit_7: Form of Inheritance, Substitution Flashcards

1
Q

What are 9 forms of Inheritance?

A
  1. Specialization: subclass is a specialized form
  2. Specification: parent describes required behavior (abstract)
  3. Construction: subclass constructs something different
  4. Generalization: subclass is a more general object
  5. Extension: subclass adds new methods, but no overriding
  6. Limitation: removal of functionality in the subclass
  7. Containment: use another class as instance variable
  8. Variance: forcing parent-child relationship instead of creating abstract class on top
  9. Combination: when using multiple inheritance
    No substitution (No Is-a relationship): 3,4,6,8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What type of inheritance is this? Is it
a good or bad form of inheritance?
Java
class Node{…}
class MyList{
private Node first;
public Node getFirst(){
return first;
}
}

A

Type: ✅ Composition (not inheritance)

MyList has a Node — this is called composition (a “has-a” relationship).

There is no extends keyword, so MyList does not inherit from Node.

=> good design! OO prefer composition: more modular, flexible

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

What type of inheritance is this? Is it
a good or bad form of inheritance?
Java
class Car{
private int age;
public int getAge(){return age;}
public void setAge(int a){age = a;}
public void startCar(){…code to start the car…}
}
class EnginelessCar extends Car{
public void startCar(){}
}

A

specialization
Bad: Violates Liskov Substitution Principle — EnginelessCar shouldn’t inherit behavior it can’t support.

Fix: Refactor using composition or better class hierarchy (e.g., both inherit from a general Vehicle class).

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

What type of inheritance is this? Is it
a good or bad form of inheritance?
C++
class Adult{…};
class Student{…};
class GradStudent : public Adult, public Student{…}

A

Type: Combination

Good/Bad: ⚠️ Potentially bad – can cause ambiguity (e.g., if both Adult and Student define the same method like getID())

Use carefully with virtual inheritance to avoid the diamond problem

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

What type of inheritance is this? Is it
a good or bad form of inheritance?
Java
class Bear{
public void eat(){…}
public void sleep(){…}
}
class Person extends Bear{
public void cook(){…}
}

A

Type: Specialization
Bad design: A Person is not a type of Bear, so using inheritance here creates a misleading “is-a” relationship.
Better approach: Use composition (containment) instead, where a Person could have a Bear as a member.

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

What type of inheritance is this? Is it
a good or bad form of inheritance?
Java
abstract class 2DShape{
public abstract double calculateArea();
}
class Circle extends 2DShape{

}

A

Type: Specification

Good: 2DShape describes a required behavior (abstract method calculateArea()), and Circle specifies the behavior for that shape. it uses inheritance to ensure that all shapes (like Circle) will have a method to calculate area.

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

What type of inheritance is this?
How would you reorganize?
Java
class Tiger{…}
class Lion extends Tiger{…}

A

Type: Specialization :Lion is a specialized form of Tiger. It represents a more specific type of Tiger.

Reorganize as a more general class:
Since Lion and Tiger are both types of big cats, it might make sense to introduce a more general BigCat class, with shared properties and methods, and then have Lion and Tiger inherit from it.

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

What type of inheritance is this Is it a
good or bad form of inheritance?
Java
class Feline{…}
class Lion extends Feline{…}

A

Specialization: Lion is a specialized form of Feline. A Lion is a specific type of Feline, and it inherits properties and behaviors from the Feline class.

Good form of inheritance (in this case)

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