Intro
OOP is a programming paradigm that models state as objects. These objects often represented by classes have fields, variables that store the state of a particular instance of that object.
Classes can have functions associated to them called Methods. These methods work on the state of the class instance, but not all necessarily mutate the instance.
What and How
Classes
Classes represent an object, encapsulating all the fields (instance variables) representing state of a particular instance of an object.
The classic example is a car with 4 wheels, an engine, fuel type, transmission, number of seats…etc. Each different car, instance of the Car
class would have their own state, representing these properties (this is what Python calls fields) whether they’re unique to it or not.
Here’s a rather simple but verbose example:
class Car {
Float tireOne, tireTwo, tireThree, tireFour;
String engineType;
String fuelType;
public Car(Float p1, Float p2, Float p3, Float p4, String engine, String fuel) {
this.engineType = engine;
this.tireOne = p1;
this.tireTwo = p2;
this.tireThree = p3;
this.tireFour = p4;
this.fuelType = fuel;
}
public String drive() {
return "Vroooom!";
}
}
Methods
In the previous example, the Car
class has a drive()
function within its scope, this is known as a method.
Methods are functions associated with a class and encapsulated by it due to a logical or semantic relation. I.e. it makes sense for the method to belong to the class. A car will drive…
- Some methods return values of instance variables, these are known as “getters”.
- Some mutate instance variables, changing state and are known as “setters”.
Statics
Most OOP languages support what are called Static Members. These are constants and methods that belong to the class itself rather than any particular instance of it. Allowing users to call these methods without instantiating an instance of the object.
Not all languages support static constants.
Inheritance
When a class is defined, referred to as the superclass or parent, the subclasses (children), can extend it. This means they inherit its members, and can eve override them.
A subclass can only have One parent (at least in Java dunno about other languages) but a superclass can have many subclasses.
Overriding
Is the process of subclasses overriding methods with their own implementation that’s more specific and catered to the child class.
Polymorphism
When subclasses override the members of their superclass, the members of the sub are potentially different at run-time than they are at compile-time.
During runt time, when a method of a subclass instance is invoked the runtime will lookup the method, and see if it’s overridden or not. If it is, then it’ll use the subclass’ method, otherwise it’ll use the parents.
Moreover, the runtime will look up the run-time type of the variable.
- Static Type: the declared type of a variable (The compiler’s job is to check for static-type violations) → Variable data type for the compiler.
- Dynamic Type : the type of the object a variable actually points to (it can be an instance of a subclass!) → Actual data type being stored
SOLID Principles
The SOILID Principles of OOP are collection of five principles, designed to guide developers to write reusable and coherent Object-Oriented projects.
Refers to when a dependency of a class, method, or function is passed into it from the outside instead of initializing it within scope. It’s a technique that promotes Separation of Concerns, decreases coupling while increasing cohesion.
Uses
Resources
- For books on the topic see [OOP Design Pattern books](OOP & Design Patterns).
- COMP2522 OOP II.
- Polymorphism
- COMP2522 Week 5
- Dependency Injection - Wikipedia