Inheritance Flashcards

1
Q

When to use a abstract class as superclass

A

When a subclass definitely has an “is-a” relationship with the superclass.

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

Benefits of using an abstract class

A

Superclasses can have methods added to the subclass without changing the subclass.

Abstract classes can define attributes that are not static and not final.
Methods do not have to be public.

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

When to use an interface over an abstract class

A

When a class will use an interface or the class is a “has-a” relationship

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

Benefits of using an interface

A

Allows code to be loosely coupled with dependencies allowing different implementations of the interface to be used at runtime.
Classes can use multiple interfaces.
This allows for greater testability by swapping out mock classes.
It follows the Liskov Substitution Principle and Open/Closed Principle of SOLID

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

What is inheritance

A

A mechanism to derive a class from another class for a hierarchy of classes that share a set of attributes and methods.

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

What are the modifiers

A

Public: Can be accessed by anyone
Protected: Can only be accessed by itself, child, and friend classes
Private: Can only be accessed by itself
No Modifier: Can be accessed by itself and classes in the same package

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

final keyword

A

Ensure that a subclass does not override a method

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

Issues with using an abstract class over an interface

A
Subclasses can only inherit one class. 
Since subclasses are tightly-coupled to their superclass, any change to the abstract class causes changes to any other subclass inheriting from it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly