Ch.11 Flashcards

Inheritance and Polymorphism

1
Q

What is Inheritance?

A

Defining a new class by extending an existing class. (like child class from parent class)

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

What is a Superclass?

A

AKA “parent class”,
A general class that can be used to create specialized classes (subclasses).

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

What is a Subclass?

A

AKA “child class”,
A specialized class that is created using a general class (Superclass).

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

What are Subtypes and Supertypes?

A

Ex: Circle is “subtype” of GeometricObject,
GeometricObject is “supertype” for Circle.

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

How do you write the class declaration for a Subtype?

A

public class Subclass extends Superclass

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

Is it possible to change parent/supertype data fields from the child/subtype class?

A

Yes, using the parent/supertype’s public methods: getter/setter/etc

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

Multiple Inheritance

A

Capability NOT ALLOWED BY JAVA.
Means a subclass can be derived from multiple classes.

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

Single Inheritance

A

Restriction only allowing a subclass to be derived from a single superclass.

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

“super” Keyword

A

Refers to the superclass of the class the keyword appears in. Used to call the superclass constructor and reference superclass’s accessible(public) members.

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

Calling Superclass Constructors from Subclass

A

Superclass constructors aren’t inherited by a subclass. They can only be invoked by subclass constructors using keyword ‘super’.
Ex: super() or super(arguments);

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

What Superclass Constructor is invoked automatically by a Subclass Constructor?

A

The Superclass Default Constructor.
super();

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

Why should every class contain a no-arg/default constructor?

A

It’s easier to extend the class and avoid errors with reuse.

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

What constructor does a subclass invoke first?

A

The superclass constructor.
If that superclass is derived from its own superclass, then that ‘ancestor’ class would be invoked first.

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

Constructor Chaining

A

A subclass invokes its superclass’s constructor before it performs other tasks.
If that superclass has its own superclass, then that parent class would also invoke the parent class before doing its own functions.

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

Calling Superclass Methods

A

super.method(arguments);

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

Overriding Superclass Method in Subclass

A

Method must be defined in subclass with same signature as superclass. This only goes for accessible/public superclass methods.

17
Q

Static Methods

A

Static methods cannot be overridden. If Superclass and Subclass define the same static method, then the superclass’s is hidden unless specifically invoked:
SuperClassName.staticMethodName.

18
Q

Method Overriding

A

Subclass modifies implementation of method defined in superclass.

19
Q

Overload vs Override

A

Override has the same signature, Overload has the same function name, but different parameter list.

20
Q

Polymorphism

A

Supertype variable can refer to a subtype object.

21
Q

Dynamic Binding

A

An object’s class will be determined at runtime, like in the case of the toString() example.

22
Q

Casting Object

A
23
Q

Create Array List from an Array

A

ArrayList<array_type> list = new ArrayList<>(Arrays.asList(array))</array_type>

24
Q

Create Array from an Array List

A

type[] array = new type[list.size()];
list.toArray(array1);

25
Q
A