Object-oriented programming (OOP)  in Java

Photo by Andrew Neel on Unsplash

Object-oriented programming (OOP) in Java

What are the components of OOP in Java

Object-oriented programming (OOP) is a programming paradigm that is used to develop complex applications. OOP is based on the concept of objects, which can contain data and methods. Java is an object-oriented programming language that has been widely used for developing various applications, including web applications, desktop applications, mobile applications, and more.

In this article, we will explore object-oriented programming in Java and provide examples of how to use OOP concepts in Java.

Classes and Objects

In Java, everything is an object, and every object belongs to a class. A class is a blueprint or a template for creating objects. It defines the properties and methods that an object will have. An object is an instance of a class. It is created from the blueprint provided by the class.

Here is an example of a class in Java:

public class Car {
   private String make;
   private String model;
   private int year;
   public void setMake(String make) {
      this.make = make;
   }
   public void setModel(String model) {
      this.model = model;
   }
   public void setYear(int year) {
      this.year = year;
   }
   public String getMake() {
      return make;
   }
   public String getModel() {
      return model;
   }
   public int getYear() {
      return year;
   }
}

In this example, we have created a class called Car. The class has three properties: make, model, and year. We have also defined methods to set and get the values of these properties.

To create an object of this class, we can use the new keyword and the class constructor:

Car myCar = new Car();

This will create an object of the Car class and assign it to the myCar variable.

Inheritance

Inheritance is a mechanism that allows a class to inherit the properties and methods of another class. The class that inherits the properties and methods is called the subclass, and the class that is being inherited from is called the superclass.

Here is an example of inheritance in Java:

public class ElectricCar extends Car {
   private int batteryCapacity;
   public void setBatteryCapacity(int batteryCapacity) {
      this.batteryCapacity = batteryCapacity;
   }
   public int getBatteryCapacity() {
      return batteryCapacity;
   }
}

In this example, we have created a subclass called ElectricCar, which inherits from the Car class. The ElectricCar class has an additional property called batteryCapacity. We have also defined methods to set and get the value of this property.

We can create an object of the ElectricCar class in the same way as we created an object of the Car class:

ElectricCar myElectricCar = new ElectricCar();

Polymorphism

Polymorphism is a mechanism that allows objects to be treated as if they are of multiple types. This means that a variable of a superclass type can hold an object of a subclass type.

Here is an example of polymorphism in Java:

Car myCar = new ElectricCar();

In this example, we have created an object of the ElectricCar class and assigned it to a variable of the Car type. This is possible because the ElectricCar class is a subclass of the Car class. We can use the myCar variable to call methods of the Car class and the ElectricCar class.

Encapsulation

Encapsulation is a mechanism that allows the properties and methods of a class to be hidden from other classes. This is done to ensure that the class can only be accessed through its public methods.

In Java, encapsulation is achieved using access modifiers. There are four types of access modifiers in Java:

Public:

The property or method can be accessed from any class.

Private:

The property or method can only be accessed from within the same class.

Protected:

The property or method can be accessed from within the same class and any subclass.

Default:

The property or method can be accessed from within the same package.

Here is an example of encapsulation in Java:

public class Employee {
   private String name;
   private int age;
   private double salary;
   public Employee(String name, int age, double salary) {
      this.name = name;
      this.age = age;
      this.salary = salary;
   }
   public String getName() {
      return name;
   }
   public int getAge() {
      return age;
   }
   public double getSalary() {
      return salary;
   }
}

In this example, we have created a class called Employee with three properties: name, age, and salary. These properties are marked as private, so they can only be accessed from within the same class. We have also defined public methods to get the values of these properties.

Abstraction

Abstraction is a mechanism that allows us to hide the implementation details of a class from the outside world. This is done to simplify the code and make it easier to use.

In Java, abstraction is achieved using abstract classes and interfaces. An abstract class is a class that cannot be instantiated, and it can contain abstract methods. An abstract method is a method that does not have an implementation. It is up to the subclasses to provide an implementation for the abstract method.

Here is an example of an abstract class in Java:

public abstract class Shape {
   public abstract double area();
}

In this example, we have created an abstract class called Shape. The Shape class has an abstract method called area(). This method does not have an implementation.

We can create a subclass of the Shape class and provide an implementation for the area() method:

public class Rectangle extends Shape {
   private double width;
   private double height;
   public Rectangle(double width, double height) {
      this.width = width;
      this.height = height;
   }
   public double area() {
      return width * height;
   }
}

In this example, we have created a subclass of the Shape class called Rectangle. We have provided an implementation for the area() method.

Conclusion

Object-oriented programming is a powerful programming paradigm that is used to develop complex applications. Java is an object-oriented programming language that provides support for all the core OOP concepts, including classes and objects, inheritance, polymorphism, encapsulation, and abstraction.

In this article, we have provided examples of how to use these OOP concepts in Java. We hope that this article has been helpful in providing an understanding of OOP in Java.