Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming (OOP) in Java

Key Concepts of Inheritance, Polymorphism, Abstraction, and Encapsulation in Java

Updated
5 min read
Understanding Object-Oriented Programming (OOP) in Java

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to create modular, reusable, and scalable software. Java, being one of the most popular programming languages, heavily relies on OOP principles. In this blog, we will dive into the core concepts of OOP in Java, including classes, objects, inheritance, polymorphism, encapsulation, and abstraction.

  1. Classes and Objects

At the heart of OOP are classes and objects. A class is a blueprint for creating objects, providing initial values for state (member variables or fields) and implementations of behavior (member functions or methods). An object is an instance of a class.

Example:

// Defining a class
// Defining a class

public class Car {

// Fields (state)

String color;

String model;

int year;

// Constructor

public Car(String color, String model, int year) {

this.color = color;

this.model = model;

this.year = year;

}

// Method (behavior)

public void displayDetails() {

System.out.println("Car model: " + model + ", Year: " + year + ", Color: " + color);

}

}

// Creating an object

public class Main {

public static void main(String[] args) {

Car myCar = new Car("Red", "Toyota", 2020);

myCar.displayDetails();

}

}

In this example, Car is a class with fields color, model, and year. The constructor initializes these fields, and the method displayDetails prints the car's details. In the Main class, we create an object myCar and call its displayDetails method.

  1. Inheritance

Inheritance is a mechanism where one class inherits the fields and methods of another class. It promotes code reusability and establishes a relationship between the parent class (superclass) and the child class (subclass).

Example:

// Superclass

public class Vehicle {

String brand;

public void honk() {

System.out.println("Beep Beep!");

}

}

// Subclass

public class Bike extends Vehicle {

String modelName = "Harley";

}

public class Main {

public static void main(String[] args) {

Bike myBike = new Bike();

myBike.brand = "Harley-Davidson";

myBike.honk();

System.out.println(myBike.brand + " " + myBike.modelName);

}

}

In this example, Vehicle is a superclass with a field brand and a method honk. The Bike class inherits from Vehicle and adds its own field modelName. The Main class creates an object myBike, sets its brand, calls the inherited honk method, and prints the bike's details.

  1. Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon. In Java, polymorphism can be achieved through method overriding and method overloading.

  • Method Overriding:

    Subclass provides a specific implementation of a method that is already defined in its superclass.

  • Method Overloading: Multiple methods have the same name but different parameters within the same class.

Example:

// Method Overriding

class Animal {

public void sound() {

System.out.println("Animal makes a sound");

}

}

class Dog extends Animal {

@Override

public void sound() {

System.out.println("Dog barks");

}

}

// Method Overloading

class MathOperations {

public int add(int a, int b) {

return a + b;

}

public double add(double a, double b) {

return a + b;

}

}

public class Main {

public static void main(String[] args) {

Animal myDog = new Dog();

myDog.sound();

MathOperations math = new MathOperations();

System.out.println(math.add(2, 3)); // Outputs 5

System.out.println(math.add(2.5, 3.5)); // Outputs 6.0

}

}

In this example, the Animal class has a sound method, which is overridden by the Dog class to provide a specific implementation. In the MathOperations class, the add method is overloaded to handle both integer and double parameters.

  1. Encapsulation

Encapsulation is the technique of wrapping the data (variables) and code (methods) together as a single unit. It restricts direct access to some of the object's components, which can only be accessed through public methods.

Example:

public class Person {

private String name; // private = restricted access

// Getter

public String getName() {

return name;

}

// Setter

public void setName(String newName) {

this.name = newName;

}

}

public class Main {

public static void main(String[] args) {

Person person = new Person();

person.setName("John");

System.out.println(person.getName());

}

}

In this example, the Person class has a private field name and public getter and setter methods. This ensures that the name field can only be accessed and modified through the getName and setName methods, providing controlled access to the data.

  1. Abstraction

Abstraction is the concept of hiding the complex implementation details and showing only the essential features of the object. In Java, abstraction can be achieved using abstract classes and interfaces.

Example:

// Abstract Class

abstract class Animal {

public abstract void animalSound();

public void sleep() {

System.out.println("Zzz");

}

}

// Subclass (inherit from Animal)

class Pig extends Animal {

public void animalSound() {

System.out.println("The pig says: wee wee");

}

}

public class Main {

public static void main(String[] args) {

Pig myPig = new Pig();

myPig.animalSound();

myPig.sleep();

}

}

In this example, Animal is an abstract class with an abstract method animalSound and a regular method sleep. The Pig class inherits from Animal and provides an implementation for the animalSound method. The Main class creates an object myPig, calls its animalSound and sleep methods.

Conclusion

Understanding the principles of Object-Oriented Programming in Java is crucial for writing efficient, maintainable, and scalable code. By mastering classes, objects, inheritance, polymorphism, encapsulation, and abstraction, you can leverage the full potential of Java's OOP capabilities. Start incorporating these concepts into your Java projects and experience the power of OOP firsthand!

Happy coding!

More from this blog

Santosh's blog

17 posts