Fundamental Concepts

The four fundamental concepts of OOP are:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Abstraction

This means to simplify complex reality, by considering only the properties and behaviours of an object that are appropriate to the problem being solved and ignoring other, irrelevant, details. For example, a person has a name, gender and a post code. They also have eyes of a particular colour, but this property is of no interest in an application designed to calculate their wages.

Encapsulation

This means to hide the complexity of the inner workings of an object from the programs, and programmers, that make use of it. It is also often referred to as information hiding, because the data contained within an object, and the functions that manipulate that data, are bound together, and therefore safe from outside interference.

To write code that will create an object from a class, then set its properties and call its methods, it is not necessary to understand the inner workings of the class.  If a class already exists, all a programmer really needs to know are the names of the properties and methods available, and any data that needs to be supplied when they are called; in other words, all a programmer really needs to know about is the public interface of the class.  In big software development projects, that employ developers with different roles and different skill sets, it’s common for senior programmers to write classes that will be used by junior programmers.  For example, some programmers might develop a set of class libraries to encapsulate a range of complex business functionality.  Compiled versions of these classes can then made available to other programmers who only need knowledge of the class’ interfaces.  Indeed some organisations specialise in developing classes that can be used by software developers in other organisations.  The implementation code of a class can remain a mystery to some programmers.  Consider this, you don’t need to know how a car works in order to drive it.

Inheritance

This is the the ability to define additional classes based on an existing class.  For example, a Person class will include properties and methods of a Person object. An Employee is a type of Person so an Employee class can therefore be based on the Person class, thereby inheriting the properties and methods defined within the Person class.  The Employee class can then be extended with additional properties and methods specific to an Employee, such as job role and salary.  A Customer is also a type of Person.  A Customer class can also be based on the Person class, then extended with additional properties and methods specific to a Customer, such as credit limit. Inheritance is said to define type of relationships between classes; an Employee is a type of Person, a Customer is also a type of person. When class B inherits from class A, class A is referred to as the ‘super-class’, or ‘parent class’, and class B is referred to as the ‘sub-class’, or ‘derived class’. If there are multiple classes in a chain of inheritance, the class at the start of the hierarchy is referred to as the ‘base class’.

Polymorphism

Literally, this means ‘many forms’.  In OOP terms, polymorphism means that classes can have different functionality while sharing a common interface.  When one class inherits from another, the sub-class inherits all of the properties and methods of the super-class.  It is possible for the sub-class to override the inherited properties and methods with different versions of its own.  This means that different classes within the inheritance hierarchy can implement the same properties or methods in different ways.  For example, you may have an Employee class with a method to calculate pay based on hours worked, rate of pay and tax payable.  A Manager class (which is a type of Employee) may then inherit this method from the Employee class, but override it with a new version because a manager’s pay is calculated in a different way.  Consider this: a hair stylist, a surgeon and an actor, all know what to do when you say “cut”.