Inheritance Flashcards

1
Q

the class that inherits from another class

A

Subclass (child)

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

the class being inherited from

A

Superclass (parent)

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

What keyword is use to inherit from a class?

A

extends

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

It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.

A

Inheritance

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

Which class is the subclass (child) and the superclass (parent) in the following code?

class Vehicle {
protected String brand = “Ford”; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println(“Tuut, tuut!”);
}
}

class Car extends Vehicle {
private String modelName = “Mustang”; // Car attribute
public static void main(String[] args) {

// Create a myCar object
Car myCar = new Car();

// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();

// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);   } }
A

Vehicle - superclass (parent)
Car - subclass (child)

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

If you don’t want other classes to inherit from a class, what keyword must be used?

A

final

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

What will happen in the following code?

final class Vehicle {

}

class Car extends Vehicle {

}

A

Java will generate an error when you try to access a final class

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

used to have general class to specific class by using the word extends

A

Inheritance

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

Which class is the subclass (child) and superclass (parent) below?

class A {

}

class B extends A {

}

A

Class A - Superclass/Parent
Class B - Subclass/Child

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