Object-Oriented Programming (OOP)

Chanidu Madalagama
6 min readFeb 5, 2023

--

Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of objects, which are instances of classes. OOP provides a way to structure code in a way that makes it easier to understand, maintain, and reuse. It is one of the most widely used programming paradigms and is used to develop a wide variety of software applications.

In this article, we will discuss the main concepts of OOP and provide examples to illustrate each one.

  1. Abstraction

Abstraction is the process of hiding the implementation details of an object and exposing only the essential features to the outside world. In OOP, abstraction is achieved through classes and objects. A class represents a blueprint for creating objects, and objects are instances of a class. The class defines the properties and methods of the object, and the object provides a way to access the properties and methods.

Here’s an example of abstraction in Java:

class Car {
private String make;
private String model;
private int year;

public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}

public void startEngine() {
System.out.println("Engine started");
}

public void drive() {
System.out.println("Driving");
}
}

Car myCar = new Car("Toyota", "Camry", 2020);
myCar.startEngine();
myCar.drive();

In this example, the Car class abstracts the details of a car into an object-oriented class. The startEngine and drive methods are the abstracted actions that can be performed on a car, while the implementation details are hidden from the user. When creating an instance of the Car class, the user only needs to provide the make, model, and year of the car, making it easy to work with car objects in a program.

2.Encapsulation

Encapsulation is the mechanism of grouping related data and functions into a single unit, called an object. Encapsulation provides a way to protect the data of an object from outside access and modification, ensuring the data is only accessed and modified through the object’s methods. This helps to maintain the integrity of the data and ensure that changes are made in a controlled, consistent manner.

Here’s an example of encapsulation in Java:

class BankAccount {
private int balance;

public BankAccount(int balance) {
this.balance = balance;
}

public void deposit(int amount) {
balance += amount;
}

public void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient balance");
}
}

public int getBalance() {
return balance;
}
}

BankAccount account = new BankAccount(1000);
System.out.println("Initial balance: " + account.getBalance());
account.deposit(500);
System.out.println("After deposit: " + account.getBalance());
account.withdraw(2000);
System.out.println("After withdraw: " + account.getBalance());

In this example, the BankAccount class encapsulates the data and behavior related to a bank account. The `balance

3.Inheritance

Inheritance is the mechanism by which a new class is derived from an existing class. The new class inherits all the properties and methods of the existing class, and can also add new properties and methods of its own. This allows for code reuse and makes it easier to maintain and extend the code.

Here’s an example of inheritance in Java:

class Animal {
private String name;
private int age;

public Animal(String name, int age) {
this.name = name;
this.age = age;
}

public void eat() {
System.out.println(name + " is eating");
}

public void sleep() {
System.out.println(name + " is sleeping");
}
}

class Dog extends Animal {
private String breed;

public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}

public void bark() {
System.out.println(name + " is barking");
}
}

Dog myDog = new Dog("Max", 5, "Labrador");
myDog.eat();
myDog.sleep();
myDog.bark();

In this example, the Animal class represents a blueprint for creating objects that represent animals. The Dog class inherits from the Animal class, and adds a breed property and a bark method to the Animal class. When creating an instance of the Dog class, the user only needs to provide the name, age, and breed of the dog, making it easy to work with dog objects in a program.

4.Polymorphism

Polymorphism is the mechanism by which objects of different classes can be treated as objects of a common class. In OOP, polymorphism allows methods to be defined with the same name in different classes, and when an object is invoked with that method, the appropriate method for the object’s class is called. This allows for a more generic and flexible way of programming, making it easier to work with objects of different classes in a program.

Here’s an example of polymorphism in Java:

class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}

class Circle extends Shape {
public void draw() {
System.out.println("Drawing a circle");
}
}

class Square extends Shape {
public void draw() {
System.out.println("Drawing a square");
}
}

Shape shape1 = new Circle();
Shape shape2 = new Square();

shape1.draw();
shape2.draw();

In this example, the Shape class defines a draw method, which is overridden by the Circle and Square classes. When creating objects of the Circle and Square classes, they can be treated as objects of the Shape class, and the appropriate draw method for each object's class is called. This allows for a more generic and flexible way of working with objects of different shapes in a program.

In conclusion, these four concepts — abstraction, encapsulation, inheritance, and polymorphism — form the foundation of OOP. By understanding these concepts and

5.Composition

Composition is a relationship between two classes where one class contains an instance of another class as one of its members. This allows for code reuse, and makes it possible to create complex objects from simpler objects.

Here’s an example of composition in Java:

class Engine {
private int horsepower;

public Engine(int horsepower) {
this.horsepower = horsepower;
}

public void start() {
System.out.println("Engine started with " + horsepower + " horsepower");
}
}

class Car {
private Engine engine;
private String make;
private String model;

public Car(Engine engine, String make, String model) {
this.engine = engine;
this.make = make;
this.model = model;
}

public void drive() {
engine.start();
System.out.println("Driving " + make + " " + model);
}
}

Engine v8Engine = new Engine(400);
Car sportsCar = new Car(v8Engine, "Ferrari", "488 GTB");
sportsCar.drive();

In this example, the Engine class represents the engine of a car, and the Car class contains an instance of the Engine class as one of its members. When creating an instance of the Car class, the user must provide an instance of the Engine class, making it possible to create different car objects with different engines. This allows for a more flexible and modular way of working with car objects in a program.

6.Association

Association is a relationship between two classes where one class uses an instance of another class. This allows for code reuse and makes it possible to create complex objects from simpler objects.

Here’s an example of association in Java:

class Student {
private String name;
private int id;

public Student(String name, int id) {
this.name = name;
this.id = id;
}

public String getName() {
return name;
}

public int getId() {
return id;
}
}

class Course {
private String name;
private int code;
private Student student;

public Course(String name, int code, Student student) {
this.name = name;
this.code = code;
this.student = student;
}

public String getName() {
return name;
}

public int getCode() {
return code;
}

public String getStudentName() {
return student.getName();
}
}

Student student = new Student("John Doe", 12345);
Course course = new Course("Object-Oriented Programming", 101, student);
System.out.println("Student: " + student.getName());
System.out.println("Course: " + course.getName());
System.out.println("Student in course: " + course.getStudentName());

In this example, the Student class represents a student, and the Course class uses an instance of the Student class as one of its members.

--

--

Chanidu Madalagama
Chanidu Madalagama

Written by Chanidu Madalagama

A dedicated and diligent young individual who’s driven by a thirst for challenge. I have more than 1 year working experience as a Mobile Application developer.