Lecture 3 Flashcards
Object Oriented Design Principles
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Encapsulation
Grouping related variables and function to reduce complexity and increase reusability.
Classes/Objects.
Abstraction
Hide details and show the essentials. Isolate the impact of changes.
Private helper methods. Only interactable with public methods. Use interfaces to group related methods together.
Inheritance
Eliminate redundant code.
Superclasses and subclasses.
Remember, subclasses inherit all public/protected attributes and methods from their superclass.
Polymorphism.
Refactor ugly if and switch/case statements.
2 kinds of polymorphism.
Static (compile time): Method overloading.
Dynamic (runtime): Method overriding through inheritance.
Substitution principle.
States that you can always use a subclass when a superclass object is expected.
Polymorphism benefits.
Method calls are always determined by the type of the object, not the variable containing the object reference.
This is dynamic method lookup. Lets us treat objects of different classes in a uniform way.
This is polymorphism. Ask multiple objects to carry out a task, and they each do so in their own way.
This makes programs easily extendable.
What is an interface? What are the benefits?
A group of related methods with empty bodies. This lets us program uniformly with an interface type.
The implementation is deferred to subclasses.
Helps with program modification and extension.
Subclasses inherit…
Data and behavior from its superclass.
What is an abstract class?
Concrete classes can be instantiated.
Objects with abstract methods cannot be instantiated. But a reference variable whose type is an abstract class can be declared.
The object it refers to must be an instance of an concrete class.
Allows polymorphisms based on an abstract class.
When can a subclass be used in place of a superclass?
Always.
Can subclasses override methods defined in the superclass?
Yes, it can be overridden and reimplemented.