Codementor Events

Object-Oriented Programming in Java: Building Robust Applications

Published Mar 09, 2024
Object-Oriented Programming in Java: Building Robust Applications

Introduction

This article lays the foundations of object-oriented programming (OOP). You will learn what object-oriented programming is and what differentiates it from other programming models.
You will also learn some key OOP concepts like inheritance, polymorphism, encapsulation and abstraction.

Prerequisites

To complete the following tutorial, you will need :

  • The Java SE Development Kit 17 (JDK 17): If you don't have it already, refer to this tutorial for the installation procedure.
  • IntelliJ IDEA: If you don't have it already, refer to this tutorial for the installation procedure.

Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm inspired by real life. In this paradigm, a program is designed as a set of objects that interact with each other.
An object is characterized by its properties (attributes) and its behaviors (methods). OOP is opposed to other programming paradigms including procedural programming, functional programming
Object-oriented programming (OOP) is popular because it promotes code modularity, reusability, encapsulation, and abstraction. It simplifies complex systems, supports code organization, and models real-world scenarios effectively.

Encapsulation

Encapsulation is the ability of an object to hide its internal structure from other objects. The object thus only exposes public interfaces through which others can interact with it. All this allows the object to be able to modify its internal structure without impacting other objects. The use of access modifiers is a means of implementing encapsulation.

The most common way to implement encapsulation is through the use of accessors. The best practice is to keep the attributes of a class private and make them accessible only via accessors.

class Circle{
    //private field, not accessible from outside
    private double radius;

    //Changing the field is only possible via this public setter method : Encapsulation!
    public void setRadius(double radius){
        if(radius > 0){
            this.radius = radius;
        }else{
            System.out.println("Invalid radius value : Must be positive!");
        }
    }
    
    public double getRadius(){
        return this.radius;
    }
}

Polymorphism

Polymorphism is the ability for the same object to take several different forms. In Java, polymorphism is implemented through 2 mechanisms: method overloading and method overriding.

Method Overloading

Method overloading is a mechanism that allows a class to have multiple methods with the same name but different parameters. The Java compiler then determines which of the methods to invoke based on the number, types and order of the method's parameters. Method overloading is a form of compile-time polymorphism or static polymorphism.

In the example below the "add" method of the "Calculator" class is overloaded 4 times.

class Calculator{

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

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

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

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

    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        System.out.println(calculator.add(1,2));//Calling the method:  int add(int a, int b)
        System.out.println(calculator.add(1.3,2.3)); //Calling the method:  double add(double a, double b)
        System.out.println(calculator.add(1f,2f)); //Calling the method: float add(float a, float b)
        System.out.println(calculator.add(1.4,2.6,3.8)); //Callingthe method: double add(double a, double b, double c)
        
    }
}

Method Overriding

Method overriding is a feature of the Java language that allows a child class (subclass) to provide a specific implementation to a method already defined in the parent class (super class). When a subclass provides a specific implementation for a method that is already present in its parent class, it is said to override the method. Method overloading is a form of run-time polymorphism or dynamic binding.

class Animal{
    void makeSound(){
        System.out.println("I don't know");
    }
}

class Dog extends Animal{
    void makeSound(){
        System.out.println("Wouf Wouf");
    }
}

class Cat extends Animal{
    void makeSound(){
        System.out.println("Meow Meow");
    }
}

class TestOverride{
    public static void main(String[] args){
        Animal dog = new Dog();//Runtime polymorphism
        Animal cat = new Dog();//Runtime polymorphism
        dog.makeSound();//Calling the overridden method in Dog class
        cat.makeSound();//Calling the overridden method in Cat class
    }
}

Inheritance

Inheritance is an object-oriented programming mechanism that allows a class called a child class to inherit the attributes and methods of another class called a parent class. This concept makes code factorization possible, promoting code reuse, modularity, and the creation of a hierarchy between classes.

class Animal{
    void eat(){
        System.out.println("Animal is eating");
    }
}
class Cat extends Animal{
    void makeSound(){
        System.out.println("Meow Meow");
    }
}

class TestInheritance{
    public static void main(String[] args){
        Cat cat = new Cat();
        cat.eat();//The eat() method is inherited from the parent class Animal
        cat.makeSound();//The makeSound() is specific to the child class Cat
    }
}

Abstraction

In software engineering and computer science, abstraction is the process of generalizing concrete details,such as attributes, away from the study of objects and systems to focus attention on details of greater importance. The goal of abstraction is to focus on the relevant aspects of an object while ignoring unnecessary details.

Some of the ways of achieving abstraction are :

  • Usage of abstract data types : Abstract classes and interfaces
  • Reorganizing common behavior from groups of non-abstract classes into abstract classes using inheritance and sub-classes,
// Abstract class representing an abstract shape
abstract class Shape {
    // Abstract method for calculating area
    abstract double calculateArea();

    // Concrete method for displaying a message
    void displayMessage() {
        System.out.println("This is a shape.");
    }
}

// Concrete class representing a specific shape (Circle)
class Circle extends Shape {
    private double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double calculateArea() {
        return Math.PI * radius * radius;
    }
}

public class TestAbstraction {
    public static void main(String[] args) {
        // Creating an object of the Circle class
        Circle circle = new Circle(5.0);

        // Using abstraction to calculate the area
        double area = circle.calculateArea();
        System.out.println("Area of the circle: " + area);

        // Using abstraction to display a message
        circle.displayMessage();
    }
}

Conclusion

In this article, you learned what object-oriented programming is and what makes it different from other programming paradigms.
You also learned some important concepts of OOP i.e. inheritance, encapsulation, polymorphism and abstraction.

Originally published at https://nkamphoa.com

Discover and read more posts from Noel KAMPHOA
get started
post commentsBe the first to share your opinion
Show more replies