Unit_2 Flashcards

1
Q

When to use abstract class over in interface?

A
  1. Use Abstract Class when:
    - Classes share common code
    - Need to maintain state
    - Want to provide a base implementation
    - Classes are closely related
  2. Use Interface when:
    - Only defining behavior
    - Need multiple inheritance
    - Want to define a contract
    - Classes are unrelated but share behavior
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is inherit? when to use extend?

A

hierarchies of classes and subclasses => “is-a” relationship, we use extends

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

What is containment?

A

Containment relationship:
* when an object contains an instance of another as
one of its data members (instance variables)
=> “has-a” relationship, eg Book has an author.

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

When does abstract class need constructor?

A

Nothing to initialize unless it declare data members for inheritance purpose)

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

Why you can never create an instance of an abstract class?

A

because abstract class is template/blueprint -> contains incomplete methods, and design to be inherited from, not used directly.

But you can define >= 1 constructors

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

What is golden rule in Abstract class/method

A
  1. if you define data, other than public constants, you tell people how the’re allowed to use it. you DON’T let them use it directly
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is LSP?

A

if Child is a subclass of Parent -> instance of Parent can be substituted by instances of Child

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