OOP Concepts
Updated: 03 September 2023
Class
- A blueprint for an object
- Fields
- Parameters
- Methods
Inheritance
- What classes have in common
- Abstracted features
- Override or extend methods
- Avoid duplicate
- Anything in superclass will be reflected in all subclasses
Main
- Create objects
- Allow them to interact
Encapsulation
Allows us to validate or perform input information for a field and use Setters and Getters
Instance vs Local variables
- Instance - Defined inside of a class
- Local - Defined in a method
Polymorphism
- Allow the subclass to redifine methods in the parents
- Refer to subclasses by their superclass type
- Can treat the objects as if they’re of the superclass type
- Cannot access methods that only exist in the subclass
Abstract classes
- Cannot be an object in itself
- May contain state and/or implementation
- Subclasses cannot inherit protected fields
Interface
- Does not contain an implementation
- Only abstract methods
- Allows classes of different inheritance trees can still communicate
- Avoid interfaces that just force redefinition by subclasses
Code
Animal.java
Dog.java
Cat.java
WorkWithAnimals.java
Creature.java
Giraffe.java