Topic 4 - Inheritance Flashcards

1
Q

Define: inheritance

A

a mechanism for deriving a new class from an existing one

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

Motivation for inheritance?

A
  • can reuse existing classes

- can organize classes in hierarchical manner - can go from more general to more specific classes

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

Inheritance Terminology: The derived new class is called the _______?

A

subclass; child class; derived class

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

What does the subclass inherit?

A

Inherits the parent class or base class

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

Define: protected.

A

Class=Y | Package = Y | Subclass = Y | Subclasses in other packages = Y | Any class in other packages = N

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

Define: polymorphism

A

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

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

Overriding methods: which method is actually executed at runtime?

A

Depends on which object is used to invoke the method

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

Type of an object = when is it determined and when can it change?

A

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

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

When invoking toString() on an object, what determines what is printed and what does not determine what’s printed?

A

What’s printed depends on the actual type of the object (not the type of the variable)

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

What does dynamic binding or late binding of the variable to the type of the object mean?

A

It is not known which method should be invoked until run time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Read the following: 
public void deposit(double amount){ 
transactionCount++;
super.deposit(amount);
}
What would happen if we did not have the super reference here?
A

It would enter an infinite loop.

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

Can a class inherit from two classes the same time? (multiple inheritance)

A
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,
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

“instanceof” is a?

A

An operator.

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