Object-Oriented Programming in Plain English.

Chidozie C. Okafor
6 min readApr 16, 2022

--

dozie

When you are asked what a car is. The likely answer is that a car is an object that have four rounded wheels and can move a distance anytime a mechanical force is exerted on it. Which is obeying Newton’s 3rd law of motion.

Whether the car is a toy car or real car, the only thing you know is that the car must have some details that are hidden from naked eye, it must have some buttons that when pressed perform a particular function and also a car can appear in different models, like Toyota camry or Honda Accord. But here the make or name doesn’t matter, all we know is that the car’s behaviour and operations are similar.

So, if a car has the same implementation details, Operational functions and maybe similar class like sedan , truck and the rest, you will agree with me, that in code, we can define a car irrespective of it’s make as an Object.

Object-oriented programming offers a sustainable way to write spaghetti code. It lets you accrete programs as a series of patches.
― Paul Graham

As the name suggests, Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

We can define an Object in OOP as a class, because a class has some pattern that encourages such Object to be define. e.g We can have a sit in a car, which is a data we can define in a class, we can define a class method “start()” which will be called anytime the push start button in a car is press.

Object is a basic unit of Object Oriented Programming and represents the real life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of:

  1. State : It is represented by attributes of an object. It also reflects the properties of an object.
  2. Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
  3. Identity : It gives a unique name to an object and enables one object to interact with other objects.
  4. Method: A method is a collection of statements that perform some specific task and return result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++ and Python.
    Methods are time savers and help us to reuse the code without retyping the code.

We have shaft inside the engine of the car, which is completely hidden from us, But without it, the function start() will likely throw an error. The shaft is abstracted from us, we don’t need to know what it does in a car, but it’s work is likely to be known by the class which implements it.

When the car model is done, we can now start building different versions of the car, Toyota venza and Toyota avalon share almost the same engine, The major difference is the name, body build and interior design. So we can now say that they likely inherit those properties from the same car model or class.

All these lead us to the core principle of OOP, which are Inheritance, Abstraction, Polymorphism and Encapsulation.

Let’s Define a car class and use it to explain what those terms are.

package Car;

public class Car {
private String brand;
private String model;
private int mileage;
private int engineCapacity;

public Car(String brand, String model, int mileage, int engineCapacity) {
this.brand = brand;
this.model = model;
this.mileage = mileage;
this.engineCapacity = engineCapacity;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public int getMileage() {
return mileage;
}

public void setMileage(int mileage) {
this.mileage = mileage;
}

public int getEngineCapacity() {
return engineCapacity;
}

public void setEngineCapacity(int engineCapacity) {
this.engineCapacity = engineCapacity;
}

@Override
public
String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", model='" + model + '\'' +
", mileage=" + mileage +
", engineCapacity=" + engineCapacity +
'}';
}

private void start() {
System.out.println("Car is starting");
}

private void stop() {
System.out.println("Car is stopping");
}

public void pushStartButton() {
start();
}

public void pushBreak() {
stop();
}
}

What is happening here:

Firstly, We have Encapsulation, which is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield. As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding. Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.

Second, We have Abstraction, Data Abstraction may be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviours of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.
Consider a man driving a car. The man only knows that pressing the push start button, will start the car and accelerators will increase the speed of car or applying brakes will stop the car but he does not know about how on pressing push start the car starts or on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car.

Now Let’s have Toyota class

package Car;

public class ToyotaCamry extends Car {
public ToyotaCamry(String brand, String model, int mileage, int engineCapacity) {
super(brand, model, mileage, engineCapacity);
}

@Override
public void
pushStartButton() {
System.out.println("Welcome To Toyota Camry is starting.");
super.pushStartButton();
}

@Override
public void
pushBreak() {
System.out.println("Thank you for using Toyota Camry.");
super.pushBreak();
}

@Override
public void
setModel(String model) {
super.setModel(model);
}

@Override
public void
setBrand(String brand) {
super.setBrand(brand);
}

}

Thirdly, we have Inheritance, which is the mechanism in java and some programming language by which one class is allow to inherit the features(fields and methods) of another class.

Let us discuss some of frequent used important terminologies:

  • Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).
  • Sub Class: The class that inherits the other class is known as subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

Lastly, Polymorphism is the ability of an object to take on many forms.

Polymorphism in OOP occurs when a super class references a sub class object.

All Java objects are considered to be polymorphic as they share more than one IS-A relationship (at least all objects will pass the IS-A test for their own type and for the class Object).

We can access an object through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.

A reference variable can be declared as a class or interface type.

A single object can be referred to by reference variables of many different types as long as they are the same type or a super type of the object.

--

--

Chidozie C. Okafor
Chidozie C. Okafor

Written by Chidozie C. Okafor

Software Engineer & Backend Magician 🎩 | Python, Rust | TypeScript, Node.js | Golang | Kafka & GRPC

No responses yet