Inheritance & Polymorphism Flashcards

1
Q

What is inheritance in OOP?

A

Inheritance allows one class (subclass) to inherit attributes and methods from another class (superclass), establishing a parent-child relationship.

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

What is the purpose of the super keyword in Java?

A

The super keyword refers to the immediate parent class, used to access parent class fields, methods, or constructors.

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

How do access modifiers impact inheritance?

A

Public: Accessible everywhere.
Protected: Accessible within the same package and by subclasses.
Default: Accessible within the same package.
Private: Accessible only within the class.

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

What are the benefits of inheritance?

A

Code reusability: Avoids redundancy.
Hierarchical modeling: Reflects real-world relationships.
Extensibility: New classes can be added without affecting existing code.

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

What are the three main types of inheritance in Java?

A

Single Inheritance: One subclass inherits from one superclass.
Multilevel Inheritance: A class inherits from a subclass.
Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.

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

What is polymorphism in OOP?

A

Polymorphism allows entities to take on different forms, enabling methods or objects to behave differently based on their context.

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

What is method overloading?

A

Overloading allows multiple methods in the same class to have the same name but different parameter types or counts.

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

What is method overriding?

A

Overriding occurs when a subclass redefines a method from its superclass to provide specific behavior.

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

When should you use inheritance versus composition?

A

Use inheritance for “is-a” relationships (e.g., Dog is an Animal).
Use composition for “has-a” relationships or to increase flexibility (e.g., Car has an Engine).

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

Why prefer interfaces over abstract classes?

A

Interfaces offer more flexibility, as a class can implement multiple interfaces, while it can only extend one abstract class.

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

What is the difference between overloading and overriding?

A

Overloading: Same method name, different parameters, occurs in the same class.
Overriding: Subclass provides a new implementation for a parent class method.

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

What are best practices for using inheritance?

A

Use it for clear “is-a” relationships.
Prefer composition when possible.
Avoid deep hierarchies to reduce complexity.

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