Tech Insights

What is Object Oriented Programming (OOP)?

Joana Almeida
- 3 min. to read

Object Oriented Programming (OOP) is a popular programming model that organizes code around objects and classes, unlike procedural languages, which revolve around functions and procedures.

By focusing on the objects of a problem, instead of the procedures needed to solve it, programmers can quickly create complex structured projects that can be more easily maintained. Given this, it’s no surprise that OOP quickly became the most popular programming paradigm.

To truly know the capabilities of this paradigm, it’s important to know what it is, what it entails, and the advantages of Object-Oriented Programming. Let’s explore this topic and find out if and why you should use OOP in your projects.

What are the 3 principles of OOP?

Principles of object oriented programming

1. Inheritance

Classes can inherit from other classes. In this case, the class inherited from the other is called the child, and the one inherited from the parent. This is the main mechanism that allows the reusability of code in OOP.

A class can have any number of children, and the level of granularity you should have will depend on the project’s needs.

Some languages, such as JavaScript, use a similar method in the form of Prototyping. Prototypes can be used as a base from other objects to derive from.

2. Encapsulation

Encapsulation is heavily dependent on Abstraction to function. Abstraction defines that all information pertinent to a class should be present within the class. Also, objects should only externalize information relevant to outside objects and hide all other information for internal use. 

You shouldn’t allow outside objects to tampering with an object’s inner workings, thus avoiding mistakes from other programmers who will use your classes and code. You can have variables and methods that change the state of the object, but those changes should be opaque to outside objects.

Encapsulation then enforces Abstraction by defining the accessibility of a class’ variables and methods to outside objects, such as “available to all other objects” (public), “available to my child classes” (protected), and “available only to my class” (private). With this, programmers can control what information is visible and can be manipulated by outside objects.

3. Polymorphism

Polymorphism occurs when your objects change their behavior based on the class they derive from. This usually occurs when your objects belong to the same inheritance tree, and the child’s methods overload their parents.

What is an Object-Oriented Programming Language?

Many languages nowadays support the concept of objects and classes and thus can be used to develop OOP projects. Common and popular examples include:

What is Object-Oriented Programming with Examples?

Now that we know what OOP is and how its pillars define how it functions, it’s time to look at an example of those concepts. To do so, we’ll use a C# example. Let’s start by defining the class Animal and its contents:

class Animal{
	int age;
	
	// Constructor
	Animal(){
	
	}
	
	// Destructor
	~Animal(){
		Console.WriteLine("The animal was destroyed.");
	}
	
	
	void Move(){
		Console.WriteLine("The animal moves.");
	}
}

class Program
{
    static void Main(string[] args)
    {
	    Animal animal = new Animal();
	    animal.Move();
    }
}

This code, however, will produce an error. In C#, all functions, including constructors, need to have access modifiers or will be considered private. This means that the class Program cannot call Animal’s constructor. Let’s remedy that with the concept of encapsulation.

public class Animal{
	private int _age;
	
	// Constructor
	public Animal(){
		_age = 0;
	}
	
	// Destructor
	// Destructors are special in that they do not need access keywords.
	~Animal(){
		Console.WriteLine("The animal was destroyed.");
	}
	
	public int GetHumanAge(){
		return _age;
	}
	
	public void Move(){
		Console.WriteLine("The animal moves.");
	}
}

class Program
{
    static void Main(string[] args)
    {
	    Animal animal = new Animal();
	    animal.Move();
    }
}

Now that we’ve set Animal’s constructor as public, it now runs correctly.

The animal moves.

Time to add a little complexity to our code. Let’s define three different types of animal that will inherit from the Animal class:

public class Animal{
	private int _age;
	
	// Constructor
	public Animal(){
		_age = 0;
	}
	
	// Destructor
	// Destructors are special in that they do not need access keywords.
	~Animal(){
		Console.WriteLine("The animal was destroyed.");
	}
	
	public int GetHumanAge(){
		return _age;
	}
	
	public void Move(){
		Console.WriteLine("The animal moves.");
	}
}

public class Person : Animal(){

}

public class Dog : Animal(){

}

public class Fish : Animal(){

}

class Program
{
    static void Main(string[] args)
    {
	    Animal animal = new Animal();
	    animal.Move();
	    
		Person person = new Person();
	    person.Move();

	    Dog dog = new Dog();
	    dog.Move();
	    
	    Fish fish = new Fish();
	    fish.Move();
    }
}

But since we haven’t redefined any specific behavior in our child classes, they will automatically adopt the parent class’ behavior:

The animal moves.
The animal moves.
The animal moves.
The animal moves.

Let’s change that. To allow for polymorphism, we’ll need to define method overloading. In C#, that is accomplished through the keywords virtual and override. To allow the use of the variable _age in our child classes, we’ll also need to set it as protected instead of private.

public class Animal{
	protected int Age;
	
	// Constructor
	public Animal(){
		Age = 0;
	}
	
	// Destructor
	// Destructors are special in that they do not need access keywords.
	~Animal(){
		Console.WriteLine("The animal was destroyed.");
	}
	
	public virtual int GetHumanAge(){
		return Age;
	}
	
	public virtual void Move(){
		Console.WriteLine("The animal moves.");
	}
}

public class Person : Animal(){
	public override void Move(){
		Console.WriteLine("The person walks.");
	}
}

public class Dog : Animal(){
	public override int GetHumanAge(){
		int finalAge = 0;
		if(Age >= 1){ finalAge += 15; }
		if(Age >= 2){ finalAge += 9; }
		if(Age >= 3){ finalAge += 5 * (Age - 2);}
		return finalAge;
	}

	public override void Move(){
		Console.WriteLine("The dog trots.");
	}
}

public class Fish : Animal(){
	public override void Move(){
		Console.WriteLine("The fish swims.");
	}
}


class Program
{
    static void Main(string[] args)
    {
	    Animal animal = new Animal();
	    animal.Move();
	    
		Person person = new Person();
	    person.Move();

	    Dog dog = new Dog();
	    dog.Move();
	    
	    Fish fish = new Fish();
	    fish.Move();
    }
}

Now each animal will show a different type of movement when prompted:

The animal moves.
The person walks.
The dog trots.
The fish swims.

The Benefits of Object-Oriented Development Methods

If you want to adopt a programming paradigm like OOP to any project, it’s important to know its advantages and disadvantages. The benefits of OOP can easily help make a project come together, but not using it in the right context can bloat your project unnecessarily.

OOP Advantages

  • Problem Partitioning: OOP allows programmers to break problems into smaller ones until they are easily solvable. You build software from the ground-up.
  • Easy Expansion: Expanding on the previous point, OOP projects can have their functionality expanded upon through the integration of new classes and methods.
  • Better Problem Mapping: OOP allows programmers to approximate the subjects of the problem into objects, creating a strong metaphor for the real problem inside their code.
  • Increased Security: By promoting good principles of encapsulation, you avoid mistakes on the part of the programming team while at the same time increasing the overall security of the project.
  • Increased Code Reusability: By introducing inheritance, you allow your code to be reusable across several classes, reducing development time and possible errors. Also, through the use of libraries, programmers can easily share common code across projects.

Disadvantages of OOP

  • Performance: The amount of objects OOP requires to function will bloat a project in comparison to a procedural approach. This will inevitably lead to larger program sizes and slower execution times.
  • Requires planning: When programming using OOP, it is imperative you have a good grasp of the design of your architecture at the start to avoid problems and restructure everything later.

Conclusion

OOP is an excellent method to build software by empowering developers with a good way to organize and structure a project. With the current market demanding regular updates, OOP is in the best position to allow natural growth for a project to keep expanding.

If you’re looking for Object-Oriented Programmers for your projects, DistantJob can provide you with expert developers at competitive rates! Whether you need Python programmers, Java programmers, JavaScript programmers, or C# programmers, DistantJob can help you find people with the expertise you need and that are the perfect culture fit for your company. Don’t hesitate to contact us!

Subscribe to our newsletter and get exclusive content and bloopers

or Share this post

Are you our next superstar remote developer?

You live, breathe and eat code, and have fun figuring out how to solve problems. And you love living in South America or Eastern Europe. But you don’t feel as fulfilled as your friends in North America.

I NEED A JOB