Topic 4 - Inheritance Flashcards
Define: inheritance
a mechanism for deriving a new class from an existing one
Motivation for inheritance?
- can reuse existing classes
- can organize classes in hierarchical manner - can go from more general to more specific classes
Inheritance Terminology: The derived new class is called the _______?
subclass; child class; derived class
What does the subclass inherit?
Inherits the parent class or base class
Define: protected.
Class=Y | Package = Y | Subclass = Y | Subclasses in other packages = Y | Any class in other packages = N
Define: polymorphism
the principle that behaviour can vary, depending on the type of the object being manipulated. With inheritance, a variable can refer to objects of different types during it lifetime
Overriding methods: which method is actually executed at runtime?
Depends on which object is used to invoke the method
Type of an object = when is it determined and when can it change?
Type of an object is determined when it is created and cannot change - note that we are not talking about the type of a VARIABLE here
When invoking toString() on an object, what determines what is printed and what does not determine what’s printed?
What’s printed depends on the actual type of the object (not the type of the variable)
What does dynamic binding or late binding of the variable to the type of the object mean?
It is not known which method should be invoked until run time
Read the following: public void deposit(double amount){ transactionCount++; super.deposit(amount); } What would happen if we did not have the super reference here?
It would enter an infinite loop.
Can a class inherit from two classes the same time? (multiple inheritance)
You cannot have multiple inheritance. You can have "class extends A implements B" because B is an interface, not a class so does not violate rule of multiple inheritance,
“instanceof” is a?
An operator.