Inheritance Flashcards Preview

OOP > Inheritance > Flashcards

Flashcards in Inheritance Deck (8)
Loading flashcards...
1
Q

When to use a abstract class as superclass

A

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

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.

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

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

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.

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

7
Q

final keyword

A

Ensure that a subclass does not override a method

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.