Object Oriented Programming

Object Oriented Programming

This article is about understanding Object Oriented Programming concepts in a very simplified version.

Before going through all the technical definitions and syntaxes. let's try to understand through it our daily life situations. At the age of 25 - 30, most people are fond of cars, any person before buying any car has to check for certain specifications like engine specs(diesel or gasoline), the car's color, seating capability, and many more. These specifications same for every car right? Think about it .what will you do when you have to buy a car?

let us say 3 friends(A, B, C) who had got their first salary and want to buy a car for each of them.

Here in the above picture the Car act as a template that will be filled by different people who are buying a car. The Car template here contains only some specifications because it's just an example to make understand what are we doing in object-oriented programming. Person A filled in differently from Person B and Person C. The template is called Class, in our example Car. Person A, B, and C are known as objects.

To make it more clear we will take one more example, while applying for admission to college students are supposed to fill out the admission form. The admission form is the same for every student. Students are supposed to fill out the form. Here, the admission form without details is a Class or template similar to all the students. The forms which are filled out by students are known as objects.

So, We can say Class is a template for an Object.

Object-oriented is more exciting, if you observe that everything in the world is an object of somewhat class. For instance, every animal in the world comes under the CLASS of ANIMALS, because they are differentiated by their way of sounding, way of eating(carnivorous, herbivores, omnivores), and way of living(on land, water..).But the properties remain the same, only values of the properties changes.

If you want to understand Object-oriented programming always keep the bigger picture in mind, because Object-Orientation was intended to be closer to the real world. After reading all the above examples, now we will try to map things and give meaningful definitions to some of the words you came across in OOP's like class, object, instance, methods, attributes..etc.

Class

A class is a named group of properties and functions

or

A class is a template of an object

or

A class is a blueprint, it provides the definition for an Object, and the definition says what are the properties and the behaviors of the Object.

There are many ways of understanding a class. The basic definition says the class is a named group of properties and functions. properties are what it has and functions are what it does.

In our Car example, the information like engine, color, and number of seats are properties (what it has). Functions like drive_Forward, drive_back, open_door, on_music ..and many more, driving forward, backward, and playing music these all are the functionality of the car(i.e. what it does).

class{

properties or attributes: what it has

functions() or methods(): what it does

}

Object

An object is an instance of a class or a physical construct(physical reality) of a class. Object occupies memory whereas class does not occupy any memory, class is just a logical construct. when you declare an object of a certain class, it means you create an instance of that class. but what is an instance actually mean?

"Instance" is best understood as it relates to "class" in programming. "Classes" are used to define the properties and behavior of a category of things. E.g. A "Car" class might dictate that all cars be defined by their make, model, year, and mileage.

But you can't provide specifics about a particular car (for example, that 1978 Chevy Impala with 205,000 miles on it that your uncle Mickey drives) until you create an "Instance" of a Car. It's the instance that captures detailed information about one particular Car.

To understand the properties of an object, you need to get the concept of class from a different perspective that is Class creates a new data type that can be used to create objects. Class is a user-defined data type or non-primitive data type.

The properties of an object are state, identity, and behavior.

  • The state of an object is the value from its data type.

  • The identity of an object distinguishes one object from another. It is useful to think of an object's identity as the place where its value is stored in memory

  • The behavior of an object is the effect of data-type operations

//Class creation in java
public class Car {
    //Attributes or properties
    int numberOfSeats;
    String color;
    String engine;

    //methods or functions
    void driveForward(){
        System.out.println("car is moving forward");
    }
    void driveBackward(){
        System.out.println("car is moving backward");
    }
}
public class Main {
    public static void main(String[] args) {
       //object created with name car_A
       //car_A is known as instance of Car class
       Car car_A = new Car();
       //Accessing methods and properties through the instance
       car_A.numberOfSeats = 7;
       car_A.engine = "diesel";
       car_A.color = "Black";
       car_A.driveBackward();
    }
}

In the above code snippets if you observe Main itself is a class consisting of the main function. Remember execution of a program always starts from the main function. We have used Java because it is a purely Object-oriented programming language. People also say Java is not entirely Object-oriented because it has primitive data types (int, char, boolean..).we will understand this clearly in upcoming blogs.

In this module, we tried to understand Object-oriented programming through different examples and definitions of class, object, attributes, and methods. In upcoming modules, we look into Object-oriented programming in Java and try to understand OOP's in a simplified manner with many examples.