Week 5 Flashcards
The process of acquiring the properties (methods and fields) of another
Inheritance
Types of inheritance
Single
Multi-level
Hierarchical
Multiple
In hierarchical inheritance, Class B and C both
Inherit from class A
In multiple inheritance, Class C
inherits from both A and B
The ______ keyword is used to enforce inheritance of the properties of a class
extends
Syntax: Class Subclass extends Superclass
T/F - The vehicle superclass extends the car subclass
False
What’s an advantage of inheritance?
Lets you reuse code instead of duplicating it.
What does a subclass inherit from a sueprclass?
All members (methods, fields)
T/F - Since a car is a special type of vehicle, we can use a car object in algorithms that manipulate vehicle objects
True
Explain the substitution principle
You can always use a subclass object when a superclass object is expected.
IE. A method that processes vehicle objects can handle any kind of vehicle
Is a quiz a subclass of a question?
No, quizzes and questions are not related in that way.
You should use a single class for variation in…
Whereas you should use inheritance for variation in….
You should use a single class for variation in values
Whereas you should use inheritance for variation in behavior
Class Manager and Class Employee, who would be the superclass? Who would be the subclass?
Manager would be a subclass of employee. You must first be an employee to be a manager.
T/F - A subclass will inherit all methods, unless it overrides them
True
What is important to specify when making a subclass?
Specify what makes it different from the superclass
Do you re-declare instance variables in a subclass?
No, unless you want an instance variable that is not already part of the superclass
Does a subclass inherit private members?
Subclasses do not.
Objects of a subclass contain the private members of the superclass. Despite not being able to access them, they are there.
When inheriting methods, what do you do if the inherited behaviour is not required in the subclass?
Change the implementation of the methods
T/F - When you provide a new implementation for an inherited method, you override the method
True