Object-Oriented Programming really varies from language to language, but the core concepts are the same
Smalltalk was the first language to be called “Object Oriented” (Trivia: Simula was first language to implement the concepts)
OOP is about objects collaborating, not functions or tasks. Objects send and receive messages
C# is a class-based language, classes are the blueprints for the objects
The object, then, is an instance of a class, and it exists at runtime.
Encapsulation – writing code that is self contained. Consumers don’t need to know HOW, just that communication is possible. Expressed well through interfaces and implementation of those interfaces
Interface + Implementation leads to decoupling. If a class is a blueprint, then an interface is the description of that class.
Inheritance - (.NET is single inheritance) is-a versus has-a –> Is this something that HAS something else, or is this something that IS something else?
Keep hierarchies shallow and take advantage of abstract classes for reuse
If you have multiple sub-classes and something higher up the “chain” changes, it changes everything below it. Rather than sub-classing, use interfaces and always use the is-a test.
Polymorphism – I am one thing with many forms (Single state with many behaviors)
- Parametric Polymorphism – Generics
Single Responsibility Principle – Class has ONE thing to do and that’s it! Make sure classes only do ONE thing. Self-documenting
High cohesion/low coupling – Cohesion is a system that communicates well but doesn’t rely on other things