OOPs concepts in Java

Java is an object-oriented programming language, which means it is designed around the concept of objects. Object-oriented programming (OOP) is a programming paradigm that emphasizes the use of objects, which are instances of classes, to represent data and methods that operate on that data. In Java, there are six main OOPs concepts:


  1. 1. Encapsulation
  2. 2. Inheritance
  3. 3. Polymorphism
  4. 4. Abstraction
  5. 5. Object
  6. 6. Class


Let's discuss each of these OOPs concepts with examples:


  1. 1. Encapsulation: Encapsulation is the practice of hiding the implementation details of an object from the outside world, and only exposing a public interface that can be used to interact with the object. This is done by declaring the instance variables as private and providing public getter and setter methods to access and modify the values of the variables. Encapsulation helps in making the code more modular and easier to maintain, as changes to the implementation details of an object do not affect the code that uses the object.

Example:

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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


In this example, the Person class encapsulates the name and age of a person by declaring the instance variables as private and providing public getter and setter methods to access and modify the values of the variables.


  1. 2. Inheritance: Inheritance is the practice of creating a new class by extending an existing class. The new class, called the subclass, inherits the properties and behaviors of the existing class, called the superclass, and can add or override its own properties and behaviors. Inheritance helps in reusing code and making the code more modular.

Example:

public class Animal {
    public void eat() {
        System.out.println("Eating...");
    }
}

public class Dog extends Animal {
    public void bark() {
        System.out.println("Barking...");
    }
}


In this example, the Animal class defines a eat() method, and the Dog class extends the Animal class and adds its own bark() method.


  1. 3. Polymorphism: Polymorphism is the practice of using a single name to represent multiple related but different things. In Java, polymorphism is achieved through method overloading and method overriding. Method overloading is the practice of defining multiple methods with the same name but different parameters, while method overriding is the practice of providing a new implementation for a method that already exists in the superclass.

Example:

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

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

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

public class PolymorphismExample {
    public static void main(String[] args) {
        Shape s1 = new Circle();
        Shape s2 = new Rectangle();
        s1.draw();
        s2.draw();
    }
}


In this example, the Shape class defines a draw() method, and the Circle and Rectangle classes override the draw() method with their own implementations. The PolymorphismExample class demonstrates polymorphism by creating a Circle object and a Rectangle object and assigning them to Shape variables. The draw() method of each object is called using the Shape variables, and the appropriate implementation of draw() is called based on the actual type of the object at runtime, demonstrating polymorphism.


  1. 4. Abstraction: Abstraction is the practice of hiding the complexity of an object and only exposing a simplified interface that can be used to interact with the object. In Java, abstraction is achieved through interfaces and abstract classes. An interface is a collection of abstract methods that define a contract that a class must implement, while an abstract class is a class that cannot be instantiated and can have abstract methods, concrete methods, and instance variables.

Example:

public interface Animal {
    public void eat();
    public void sleep();
}

public class Dog implements Animal {
    public void eat() {
        System.out.println("Dog is eating...");
    }
    public void sleep() {
        System.out.println("Dog is sleeping...");
    }
}

public class AbstractionExample {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.eat();
        a.sleep();
    }
}


In this example, the Animal interface defines two methods, eat() and sleep(), which a class must implement. The Dog class implements the Animal interface and provides its own implementation for the eat() and sleep() methods. The AbstractionExample class demonstrates abstraction by creating a Dog object and assigning it to an Animal variable. The eat() and sleep() methods of the Dog object are called using the Animal variable, demonstrating that the actual type of the object is hidden and only the simplified interface is used to interact with the object.


  1. 5. Object: An object is an instance of a class that has its own state and behavior. In Java, an object is created using the new keyword followed by the name of the class, and can be assigned to a variable of the same type as the class. Once an object is created, its instance variables can be accessed and modified using the dot notation.

Example:

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

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

    public void sayHello() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }
}

public class ObjectExample {
    public static void main(String[] args) {
        Person p1 = new Person("Alice", 25);
        p1.sayHello();
        Person p2 = new Person("Bob", 30);
        p2.sayHello();
    }
}


In this example, the Person class defines a name and an age instance variable, a constructor that initializes these variables, and a sayHello() method that prints a message using the values of the variables. The ObjectExample class creates two Person objects using the new keyword and assigns them to Person variables. The sayHello() method of each object is called using the Person variables, demonstrating that each object has its own state and behavior.


  1. 6. Class: A class is a blueprint or template for creating objects. In Java, a class is declared using the class keyword followed by the name of the class, and can have instance variables, constructors, and methods. The methods define the behavior of the objects created from the class, while the instance variables define the state of the objects.

Example:

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

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

    public void sayHello() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }
}

public class ClassExample {
    public static void main(String[] args) {
        Person p1 = new Person("Alice", 25);
        p1.sayHello();
        Person p2 = new Person("Bob", 30);
        p2.sayHello();
    }
}


In this example, the Person class defines a name and an age instance variable, a constructor that initializes these variables, and a sayHello() method that prints a message using the values of the variables. The ClassExample class creates two Person objects using the new keyword and assigns them to Person variables, demonstrating that the Person class serves as a blueprint for creating objects with the same state and behavior.