What is Object-oriented programming (OOP)

Object-oriented programming (OOP) is a programming paradigm that is widely used in software development. The concept of OOP revolves around the idea of objects and classes. Java, being an OOP language, has a set of principles, practices, and techniques that are used to implement OOP concepts. In this article, we will discuss some of the OOP concepts in Java.

Classes and Objects

The core concept of OOP is classes and objects. In Java, a class is a blueprint or a template that defines the properties and methods of an object. An object is an instance of a class. For example, the class “Car” may define properties such as “make,” “model,” and “year,” and methods such as “start” and “stop.” When we create an object of the “Car” class, we can set the values of the properties and call the methods.

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

   public void start() {
      System.out.println("Starting the car...");
   }

   public void stop() {
      System.out.println("Stopping the car...");
   }
}

public class Main {
   public static void main(String[] args) {
      Car myCar = new Car();
      myCar.make = "Toyota";
      myCar.model = "Camry";
      myCar.year = 2021;
      myCar.start();
   }
}

Inheritance

Inheritance is a mechanism in 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 have additional properties and methods of its own. In Java, inheritance is implemented using the “extends” keyword. For example, the class “SUV” can be derived from the “Car” class, inheriting all its properties and methods, and can have additional properties such as “off-road capability” and methods such as “four-wheel drive.”

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

   public void start() {
      System.out.println("Starting the car...");
   }

   public void stop() {
      System.out.println("Stopping the car...");
   }
}

public class SUV extends Car {
   boolean offRoadCapability;

   public void fourWheelDrive() {
      System.out.println("Engaging four-wheel drive...");
   }
}

public class Main {
   public static void main(String[] args) {
      SUV mySUV = new SUV();
      mySUV.make = "Jeep";
      mySUV.model = "Wrangler";
      mySUV.year = 2021;
      mySUV.offRoadCapability = true;
      mySUV.start();
      mySUV.fourWheelDrive();
   }
}

Polymorphism

Polymorphism is the ability of an object to take on multiple forms. In Java, polymorphism is implemented using two mechanisms: method overloading and method overriding. Method overloading allows a class to have multiple methods with the same name, but different parameters. Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

public class Math {
   public int add(int a, int b) {
      return a + b;
   }

   public int add(int a, int b, int c) {
      return a + b + c;
   }
}

public class Main {
   public static void main(String[] args) {
      Math math = new Math();
      int result1 = math.add(2, 3);
      int result2 = math.add(2, 3, 4);
      System.out.println(result1);
      System.out.println(result2);
   }
}

Abstraction

Abstraction is the process of hiding implementation details and providing a simplified interface for the user. In Java, abstraction is implemented using abstract classes and interfaces. An abstract class is a class that cannot be instantiated, but can have abstract methods that are defined in its subclasses. An interface is a collection of abstract methods that define a set of operations that a class can perform.

abstract class Shape {
   int x;
   int y;

   public void moveTo(int newX, int newY) {
      this.x = newX;
      this.y = newY;
   }

   abstract double area();
}

class Rectangle extends Shape {
   int width;
   int height;

   public double area() {
      return width * height;
   }
}

public class Main {
   public static void main(String[] args) {
      Rectangle rect = new Rectangle();
      rect.width = 5;
      rect.height = 10;
      rect.moveTo(3, 4);
      System.out.println(rect.area());
   }
}

Encapsulation

Encapsulation is the practice of hiding implementation details and preventing direct access to the internal state of an object. In Java, encapsulation is implemented using access modifiers such as “private,” “public,” and “protected.” By setting the access level of properties and methods, we can control the access to the internal state of an object, and prevent it from being modified or accessed by other classes.

public class Person {
   private String name;
   private int age;

   public void setName(String name) {
      this.name = name;
   }

   public String getName() {
      return this.name;
   }

   public void setAge(int age) {
      this.age = age;
   }

   public int getAge() {
      return this.age;
   }
}

public class Main {
   public static void main(String[] args) {
      Person person = new Person();
      person.setName("John");
      person.setAge(25);
      System.out.println(person.getName());
   }
}

Association

Association is a relationship between two or more objects. In Java, association is implemented using instance variables. For example, if we have a “Person” class and a “Car” class, we can create an association between them by adding an instance variable of the “Car” class to the “Person” class. This allows us to model complex relationships between objects.

public class Person {
   String name;
   Car car;

   public Person(String name, Car car) {
      this.name = name;
      this.car = car;
   }
}

public class Car {
   String make;
   String model;
}  

Composition

Composition is a special form of association in which an object is composed of one or more other objects. In Java, composition is implemented using instance variables and constructors. For example, if we have a “Car” class and a “Engine” class, we can create a composition relationship by adding an instance variable of the “Engine” class to the “Car” class, and initializing it in the constructor.

In conclusion, Java is a powerful language that has been widely used for software development, and its implementation of OOP concepts has played a significant role in its popularity. By understanding the OOP concepts in Java, developers can design and implement efficient and maintainable code that is easy to understand and extend. The seven concepts of OOP discussed in this article are the building blocks

Leave a Comment

Your email address will not be published. Required fields are marked *

Exit mobile version