Object Oriented Programming

Thisara dilshan
5 min readSep 18, 2022

--

Procedural programming — Traditional programming is based on procedural programming. Procedural programming is a programming paradigm based upon procedural calls. Procedures are simply routines, subroutines, methods, or functions that carry a series of computational steps.

Procedural programming shows the following characteristics.

1. following a top-down approach

2. Data is given less importance than functions

3. Vulnerability of data is there as functions share global data.

4. Big program is divided into small modules

5. Algorithms are designed first without bothering about minute details.

Object Oriented Programming — Now we are up to the main part of the article. That is Object-oriented programming (OOP).

In oop, computer programs are looked at as a collection of objects that act on each other, where each object has a distinct role and responsibility.

OOP has four main concepts they are Encapsulation, abstraction, polymorphism, and inheritance. Let’s briefly look at each of them.

Encapsulation — Binding data and methods that act together as a single unit. As an example consider the objects, color, no of wheels, engine power, price, mileage, and methods, RequiredFuel(), all these objects and methods can bind into a single class named Vehicle.

Abstraction — It is the mechanism that represents only essential details and hides unnecessary details and internal complexity. In Java, we achieve abstraction by using Interfaces and abstract classes.

Polymorphism — It provides an entity to show/behave in many forms. so we can access different methods with one interface. There are 2 types of polymorphism, Compile-time polymorphism, and Run-time polymorphism.

Inheritance — Inheritance is when new classes show the properties of previously built classes. We can form hierarchies of classes where each class shows the special characteristics among common features. The advantage of the inheritance is that code reusability. car is a subclass of vehicle class where car shows all the features of vehicle class and also some special features such as the number of wheels is 4, air-conditioned, etc.

Object Oriented Design — In Object-oriented design we develop the system as a collection of objects that interacts with each other. Irrespective of the system type we consider the content of the system as several objects that are related to each other.

A model that is designed with Object-oriented technology is often easy to understand and can be directly related to reality. so the gap between reality and the model is small.

Advantages of OOP,

  1. Code Reusability in terms of Inheritance
  2. Easy to upgrade from one platform to another
  3. Complex projects can easily be divided into small code functions
  4. Principles of encapsulation and abstraction enable to build of secure programs
  5. Decrease software complexity
  6. More than one instance of the same class can exist without interference

The role of OOAD in the software development lifecycle — OOAD is the short term of Object oriented attribute design. It does not replace traditional approaches such as data flow, process flow, and state transition diagrams, instead, it adds new concepts of object-oriented development. In OOAD we think about the world as a collection of objects and object classes. A class is any uniquely identified abstraction (that is, model) of a set of logically related instances that share the same or similar characteristics. An object is any abstraction that models a single thing, and the term “object” is synonymous with an instance.

There are 3 types of relationships between classes. They are Inheritance, aggregation, and association. The inheritance among two classes is usually identified by the phrase “is a kind of”. The aggregation between two classes is identified by the phrase “is a part of, if the neither of them is applied between two classes and still the classes have a relationship, that fall into the association. as an example employee is associated with a company”.

Here we discussed some concepts and features of object-oriented programming. It is a widely used concept in computer programming.

Object-oriented implementation with Java.

In the above sections, we discussed some concepts of oop. Here we bring it to practical use with java. First of all, we will create a java class as a ‘vehicle’.

We discussed classes and instances that show Encapsulation. Here we defined three variables and we have developed them as private variables. To access them we need set and get functions, in the above, we have developed only functions as getColor() and setColor().

Vehicle() is a constructor it initializes the values of those attributes when an object is created.

Here we have created an object as v1, we can see initially getColor function returns the color as white, which is the initialized value by the constructor. The second time it gets the color Red.

We can define another constructor in this class, that constructor should get values as arguments.

here you can see our newly added constructor takes three parameters, but it also has the same class name. This is an instance that shows Polymorphism.

In our new v2 object we passed the three parameters for color,vnoofWheels, and brand. Here the called constructor is the newly added constructor. so by the same interface, we accessed two different constructors in the class.

And I am going to end this article from here. I will go through another article for the rest of the concepts of object-oriented programming using Java.

--

--

Thisara dilshan

Computer science undergraduate who have a great desire to sneak through boundaries of computing world.