OOP Classes and Objects

Kavindu Sandaruwan
3 min readSep 13, 2023

--

So today Im going to start talking about OOP as known as Object Oriented Programming. There are some key concepts like inheritance, composition, polymorphism, abstract, encapsulation in object oriented programming. So I will teach you these concepts one by one.

First of all we need to talk about what is a class and object. Without knowing these two things we cant start our discussion.
In object-oriented programming (OOP), classes and objects are fundamental concepts used to model and structure code.

Class: A class is a blueprint or template for creating objects. It defines the attributes (also called properties or fields) and methods (functions) that objects of that class will have. Think of a class as a set of instructions for creating objects with specific characteristics and behaviors. For example, if you were creating a program to represent cars, you might have a "Car" class that defines the attributes like "color," "make," and "model," as well as methods like "start_engine" and "stop_engine." When we combined attributes and methods together we can handle and manipulate data with these objects. Here is an example of create class in java.

public class Car{
String color; // these are attributes.
String make;

public Car(String color, String make){
this.color = color;
this.make = make;
}
void startEngine(){
//method body
}
}
I assumed that you guys have basic idea about constructors , getters, setters. And here Im going to teach you oop concepts. If you dont have proper idea about constructors and whole things please let me know , so.I can teach it as well in future.

Class

Object: An object is an instance of a class. It's a concrete, self-contained entity that is created based on the class definition. Each object created from a class has its own set of attributes and can invoke its own methods. For example, if you have a "Car" class, you can create multiple car objects, each with its unique color, make, and model. These objects can then perform actions like starting or stopping their engines independently.

Here I created object using my car class
Car myCar = new Car("Red", "Tesla");
Now you can access class members like attributes and methods using this myCar object.

myCar.startEngine() //method body will execute
myCar.make; // Tesla

In concussion: Think of a class as a blueprint for a house, and objects as actual houses built based on that blueprint. The blueprint defines the structure and features of the house (class), while each house built from that blueprint is an individual, unique instance (object).

OOP promotes code reusability, modularity, and a more intuitive way to model real-world entities and their interactions in software development.

--

--