Object oriented programming often written as OOP is a concept that every software engineer should understand. Being an engineer for a few years now, I can say that having an in-depth knowledge of OOP would set you up for success.
What is Object Oriented Programming?
Object Oriented Programming can be defined as a programming paradigm that to mimics or models entities. In OOP, we tend to reference real world entity behaviours and interactions, through the operations we perform on objects.
Why bother learning about OOP?
You may be asking yourself, why bother learning this? But the truth is, OOP have become a major anchor in modern software development. If you plan to build scalable applications, with reusable components that allows for ease of collaboration, you'd have to build on the core concepts and principles of OOP.
Since we're keeping it short, I'd go straight into the basic terminologies or core concepts of object oriented programming. And then after that, I'd discuss the principles or feature of OOP.
Core Concepts of OOP
There are 4 most commonly used terminologies or concepts of OOP.
Class: A Class is a template or blueprint that define the behaviour, structure and properties of objects. It contains the data attributes and functions or methods that operate on that data.
Object: An Object is an instance of a specified class. It contains real values instead of variables. It also represent real-world interpretation of a Class and can perform operations defined by that class.
Method: A method is function. It can be defined within a class or implemented from an interface. Methods defines the behaviour of objects and can be used to perform operations and data manipulation on objects.
Property: Properties are the data members or variables of a class. They are also knowns as fields and store the feature or data associated with an object.
Principles of OOP
There are 4 major principles of OOP and I'd briefly explain each of them below. Since we're doing an OOP summary, I'm going to try to make it as simple and short as possible.
Encapsulation
Encapsulation just like the English definition involves enforcing controls or hiding of data implementation of a class by restricting access to class methods. This principle helps to ensure that only the public methods and properties are available to the public.
For Example, you don't need to understand the inner mechanical workings of a car to drive the car, or the hardware build process of a phone to use the phone to satisfy your need.
Abstraction
Abstraction can also be explained in plain terms as reducing the complexity of an object or class by putting all the complex details in a different place, usually an abstract class or interface.
This pattern enables high level thinking, and handling complex edge cases on a surface level. In most cases, abstract classes or interfaces would define some contract or rules that connecting classes must obey.
Inheritance
I love how these principles are self explanatory like their names 😀. Inheritance like the name implies involves passing knowledge or information down within classes. When class A inherits from class B, this simply means that, class A gets the attributes and methods with in class B for free.
For Example, A class Shape which is the superclass has attribute name
and method getArea()
. If another class Circle which is the subclass inherits from Shape, it automatically get a the attribute and method within shape.
Polymorphism
Polymorphism is the ability of an objects to take different forms. There's the static polymorphism and dynamic polymorphism. Polymorphism is closely related to Inheritance, so I'm going to use the example above to explain it better.
If we had another class, say Triangle that inherits from Shape, this new class also gets the name
attribute and getArea()
function. While in the Circle class getArea()
would but implemented as πr2
where r is is the radius of the circle. In the triangle it would implemented as 1/2*b*h
where b and h are the base and height of the triangle. The implementation could also introduce a new formula depending on the type of triangle.
The ability to change the behaviour of methods or attributes in the different objects is called Polymorphic.
Conclusion
I hope this piece was helpful to you. There's still a lot to learn in OOP, but this is a summarised introduction.
Top comments (0)