Osura Hettiarachchi

Object oriented programming principles

Object oriented blog cover

This article will be using Java to explain the basic Object Oriented Principles

Objects and classes

In order to understand the object oriented programming concepts first, you should understand the concepts of objects and classes.

Class

Class is a blueprint of an object. In simpler terms, class contains the basic structure an object. Just like a blueprint of a building, class is just the design or the structure of an object.
If you think about any object in the universe, there are a set of properties or attributes and a set of actions it can perform which defines the object. Similarly, what goes inside a class can be generalized as properties and methods (actions the object can perform) that defines the object.

Object

Say we have designed a blueprint for a building that we want to create. By just creating the blueprint the said building doesn't start existing physically. In order for the building that we designed in the blueprint to become a reality we must construct the building. Similar to that what we are doing by creating the class is just creating the blueprint We must construct or instantiate an instance using the structure we defined in the class in order for an object to get created.
An Object is basically an instance of a class. Object is the actual thing that exists and that is intractable. If we want to access any method or property that we defined in the object's structure we must create an instance of the said object. Generally, we can create as many objects as we need from a class unless explicitly restricted (out of scope of this article).

class Dog {
 private String skinColor;
 private String furColor;
 private double tailLength;
 public void setFurColor(String furColor) {
  this.furColor = furColor;
 }
 public String getFurColor() {
  return this.furColor;
 }
 public void communicate() {
  // do something? ex: bark
 }
}

Inheritance

Inheritance facilitates improving reusability and maintainability of code. It allows child classes to access accessible properties and methods of the parent class when extended. Java only allows vertical inheritance which is, a child class can only extend a single parent class. But other languages like C++ support horizontal inheritance which allows extending from multiple parent classes. Implementing interfaces also a type of inheritance. Although only one class can be extended by a child class, multiple interfaces can be implemented by a single child class.
Let's look into a real world example to further understand the concept of inheritance. Let's take the relationship between Animal and Dog. For inheritance to work, the relationship between a child class and parent class should be an "is a" relationship. The example we are considering above conforms the above relationship. i.e Dog (child/sub class) is an Animal (parent/super class). If two classes do not conform the above relationship, then it means inheritance should not exist between those two classes.

Encapsulation

Encapsulation allows controlling the access to properties and methods in a class. This minimizes unintended mutations to the object instances and unintended parties from accessing the object instances. Why is encapsulation necessary? We definitely don't want our classes to be abused or the object instances being accessed by unwanted parties by a mistake or purposely which can cause security concerns and bugs. So we also should control the access to which methods and properties can be accessed from outside and even when accessed we have the ability to monitor. This is important for the general security of the application.
Java achieves encapsulation by access modifiers mainly. There are 4 main access modifiers in Java

  • Default - When no access modifier is defined for a property or a method. Default methods and properties are accessible by the sub classes and other classes within the same package
  • Public - These properties and methods can be accessed by anyone (any class) from within and outside a given package
  • Protected - These properties and methods can only be accessed by the class itself, its' subclasses and classes within the same package
  • Private - These properties and methods can only be accessed by the class itself.

Polymorphism

Polymorphism refers to multiple forms of the same thing. In programming this means the ability to provide multiple implementation to the same thing. This is a mechanism mainly introduced to cope with the ambiguity in programming. Polymorphism along with inheritance enables code reusability and reduces code repitition. Inheritance allows us to reuse logic down an inheritance hierarchy and polymorphism allows giving our own implementation at a given level of the hierarchy if required. If this was not possible inheritance alone would not be able to achieve this level of code reusability.
Polymorphism can be subdivided into two as,

  • Compile-time/static polymorphism (overloading)
  • Run-time/dynamic polymorphism (overriding)

Overloading

Concept of overloading is to provide multiple implementations to a method with the same name but with unique method signatures. Method signature refers to the method name and the list of parameters a method accepts. This uniqueness in method signatures can be achieved in two ways.

  • Number of parameters
  • Data types of the parameters

Since this polymorphism happens during the compile time this is also referred to as static polymorphism

Overriding

Concept of overriding is to provide a new implementation to an inherited method or a property from a super (parent) class within a subclass (child). This type of polymorphism happens during the run-time therefore also known as runtime polymorphism.

Abstraction

Abstraction basically refers to only exposing what is required to the outside. This means, any unwanted details such as the logic of a certain method is totally blacked out from the user. This is done since internal logic of something say a method for an example is not required to be known by the user. So any unwanted details are hidden away to minimize the cognitive complexity. It's similar to the plastic cover on a home appliance like a blender. By having a cover like this will make it easier to use the blender by a normal user without having to worry about how blender works internally.


Wanna read more? See all blogs here