What are Classes?

A class is a "template" or "blueprint", made from:

  1. Type - name describing what it is. eg User, Gate
  2. Properties / data - the attributes or state. eg Height, IsOpen
  3. Operations / methods - the behavior. eg Login, Open

What are Objects?

An object is an instance of a class.

A class may be used to instantiate many object which have the same underlying blueprint but have their own individual state/data.

Method Vs Function

Function - A block of code which is defined outside a class and therefore must be called independently.

Method - A block of code which is defined inside a class and must be called in reference to an instance of that class. "Method" is a term exclusive to OO languages and is sometimes referred to as a "member function" (like in C++).

Four Pillars of Object-Oriented Programming:

  1. encapsulation
  2. abstraction
  3. inheritance
  4. polymorphism

Encapsulation does two major things:

  1. Information Hiding
  2. Protection of Invariants

Encapsulation hides certain functionality from the rest of the application via Access Modifiers eg in C# there is private, public, protected or internal. The classic analogy of this is with a car. As a driver you do not need to know the inner workings of the car, you only need to know the controls or interface ie wheel, gear nob, pedals. Encapsulation is often seen as purely information hiding but there is more to it than that. If we go back to the analogy of the car, imagine if drivers had to manage fuel injection, air flow and spark plugs. Hiding the inner workings of the car definitely makes driving simpler but it also keeps the driver from using the internals incorrectly. Encapsulation allows a class to protect its self from changes which may result in invalid or inconsistent internal state. Therefore, encapsulation is not simply about hiding complexity, but rather exposing the functionality in a fail-safe manner. The hidden complexity comes as a result even though it is not necessarily the goal.

When designing a class how much should you hide? The rule is to hide as much as you can.

In the following example, both the Validate() and InsertToDb() methods must be called and in a specific order. By encapsulating them in the Add() method a developer can not make the mistake of calling them in the wrong order or not calling one of them.

public class Person
    public void Add(string name)

    private void Validate()
    { }

    private void InsertToDb()
    { }

Abstractions have the essential qualities of a concept but states no details. eg Animal is an abstraction because it has certain qualities which many objects could fulfil, like a Rat is an Animal.

An abstraction describes what actions a class can perform and what information the class makes publicly available. Abstractions form a type of "contract" between a client and a concrete class. A class which implements an interface has to publicly expose certain functionality or data.

In the example below all animals Eat and Poop but the term Animal does not describe what the animal actual is. An animal could be a Rat or a Lion.

public interface IAnimal

  void Eat(Food food);

  Poo Poop();


Inheritance allows a new class to adopt the properties of an existing class.

The class being inherited is known as:

The class inheriting from another class is known as:

The derived class will have all the fields, properties and methods of the base class and is extended to include further fields, properties and methods. For example, you could have a base class of "Animal" and a derived class of "Rat". The relationship between the derived class and the base class can be described with "is a". So a derived class "is a" base class eg a Rat is an Animal.

In the below example all people can Walk() and Talk() but different people can do different things such as Code() or Sell(). The basic functionality of people is abstracted to a Person class. Clients which use Person are guaranteed that every person can Walk() and Talk().

public abstract class Person
  public void Walk()
  { }

  public void Talk()
  { }

public class Developer : Person
    public void Code()
    { }

    public void DrinkCoffee()
    { }

public class Salesman : Person
    public void Sell()
    { }

    public void DrinkBeer()
    { }

Poly means "many" and morph means "changing form", so polymorphism is objects taking different forms. Polymorphism is when the same method name changes it's functionality. This can be achieved with Overriding or Overloading or Subtypes or Generics/Parametric.

subtype polymorphism or inclusion polymorphism

With subtype polymorphism many classes can inherit from the same type but provide different implementations of the same method. eg Rat and Cat each override the Talk method from an Animal Interface, then animal.Talk() may run different implementations at runtime.

public void Talk(Animal animal) // Talk(cat); or Talk(Rat);

Check out these links for more info:

My design and architecture repo