Chapter 8 Flashcards

(17 cards)

1
Q

What is inheritance in Java?

A

Inheritance allows one class (subclass) to inherit fields and methods from another class (superclass), enabling code reuse and hierarchy modeling.

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

How is inheritance implemented in Java?

A

Using the extends keyword.

```java
class Subclass extends Superclass { }
~~~

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

What does the super keyword do?

A

It refers to the parent class. Can be used to call:

  • A superclass method: super.methodName()
  • A superclass constructor: super(args) (must be the first line in constructor)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does super() do specifically in a constructor?

A

Calls the constructor of the parent class. Java inserts it implicitly only if the parent has a no-argument constructor.

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

What is method overriding?

A

A subclass provides a specific implementation of a method already defined in the superclass. The method must have the same name, return type, and parameters.

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

How do you indicate a method is overridden?

A

Use the @Override annotation above the method.

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

What is the difference between method overloading and overriding?

A
  • Overriding: Same method name & parameters in parent-child relationship.
  • Overloading: Same method name, different parameters in the same class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is polymorphism in Java?

A

The ability of a subclass object to be treated as an object of its superclass.

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

What members are inherited by a subclass?

A

All non-private fields and methods. private members are not inherited directly.

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

Can a class extend more than one class?

A

No. Java supports single inheritance only.

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

What is the final keyword used for in inheritance?

A
  • final class: cannot be subclassed.
  • final method: cannot be overridden.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the difference between an abstract class and an interface?

A
  • Abstract class: Can have fields, constructors, and some implementation.
  • Interface: Purely abstract (though Java 8+ allows default methods). Supports multiple inheritance.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When should you use abstract classes vs. interfaces?

A
  • Use abstract class for “is-a” relationships with shared implementation.
  • Use interface for unrelated types needing common behavior.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the difference between inheritance and composition?

A
  • Inheritance: “is-a” relationship (e.g., Dog is-an Animal).
  • Composition: “has-a” relationship (e.g., Car has-an Engine).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the benefits of using inheritance?

A
  1. Code reuse
  2. Logical hierarchy
  3. Polymorphism
  4. Centralized maintenance
  5. Extensibility
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is constructor chaining in inheritance?

A

When a subclass constructor calls a superclass constructor using super().

17
Q

Can a subclass override a private or static method?

A
  • private: Cannot be accessed or overridden.
  • static: Can be redefined but not truly overridden (called shadowing).