DEV Community

Chidozie C. Okafor
Chidozie C. Okafor

Posted on • Originally published at doziestar.Medium on

Object-Oriented Programming in Plain English.

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
    }

}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)