Unit_2 Flashcards
When to use abstract class over in interface?
- Use Abstract Class when:
- Classes share common code
- Need to maintain state
- Want to provide a base implementation
- Classes are closely related - Use Interface when:
- Only defining behavior
- Need multiple inheritance
- Want to define a contract
- Classes are unrelated but share behavior
What is inherit? when to use extend?
hierarchies of classes and subclasses => “is-a” relationship, we use extends
What is containment?
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.
When does abstract class need constructor?
Nothing to initialize unless it declare data members for inheritance purpose)
Why you can never create an instance of an abstract class?
because abstract class is template/blueprint -> contains incomplete methods, and design to be inherited from, not used directly.
But you can define >= 1 constructors
What is golden rule in Abstract class/method
- 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
What is LSP?
if Child is a subclass of Parent -> instance of Parent can be substituted by instances of Child