Java OOPS Overview


Java is an object-oriented programming (OOP) language, which means it follows the principles of Object-Oriented Programming to design and develop programs. OOP focuses on organizing code by using objects and classes, making the development process more modular, scalable, and easier to maintain.

Key Concepts of Object-Oriented Programming in Java:

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

1. Class:

A class is a blueprint or a template for creating objects. It defines the properties (attributes) and behaviors (methods) that an object of that class will have.

  • Syntax of a Class:
    class Car { // Fields (properties) String model; int year; // Constructor public Car(String model, int year) { this.model = model; this.year = year; } // Methods (behaviors) public void displayInfo() { System.out.println("Model: " + model + ", Year: " + year); } }

In the above example, Car is a class with fields (model and year) and a method (displayInfo()).


2. Object:

An object is an instance of a class. It is a real-world entity that has state and behavior. Objects are created from a class, and they can access the class’s methods and properties.

  • Creating an Object:
    Car myCar = new Car("Toyota", 2020);

Here, myCar is an object of the Car class with the model as "Toyota" and the year as 2020.


3. Encapsulation:

Encapsulation is the concept of bundling the data (fields) and methods (functions) that operate on the data into a single unit, the class. It also provides mechanisms for restricting access to certain components (using access modifiers like private, public, protected), which ensures data protection and prevents unauthorized access.

  • Encapsulation Example:
    public class Employee { // Private fields private String name; private int age; // Getter and Setter methods for accessing private data public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if (age > 0) { // Basic validation this.age = age; } } }

In this example, the fields name and age are encapsulated (hidden from external access), and only accessible through getter and setter methods.


4. Inheritance:

Inheritance allows one class to acquire the properties and methods of another class. The class that inherits is called a subclass (or derived class), and the class being inherited from is called a superclass (or base class).

  • Syntax for Inheritance:
    class Vehicle { public void start() { System.out.println("Vehicle started"); } } class Car extends Vehicle { // 'Car' is inheriting from 'Vehicle' public void drive() { System.out.println("Car is driving"); } } public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.start(); // Inherited from Vehicle myCar.drive(); // Defined in Car } }

In the example, the Car class inherits the start() method from the Vehicle class and adds its own method, drive().


5. Polymorphism:

Polymorphism allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. Polymorphism in Java can be of two types:

  • Compile-time Polymorphism (Method Overloading): The ability to define multiple methods with the same name but different parameter lists within the same class.

  • Run-time Polymorphism (Method Overriding): The ability to override a method of the superclass in a subclass, so that the subclass can provide a specific implementation of the method.

  • Method Overloading Example:

    class Calculator { 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) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 10)); // Output: 15 System.out.println(calc.add(5, 10, 15)); // Output: 30 } }
  • Method Overriding Example:

    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"); } } public class Main { public static void main(String[] args) { Animal myDog = new Dog(); myDog.sound(); // Output: Dog barks (overridden method) } }

6. Abstraction:

Abstraction focuses on hiding the implementation details and exposing only the essential features of an object. In Java, abstraction can be achieved using:

  • Abstract Classes: These are classes that cannot be instantiated and can have both abstract and non-abstract methods.

  • Interfaces: These define a contract that classes must follow by implementing its methods, allowing for multiple inheritance.

  • Abstract Class Example:

    abstract class Animal { // Abstract method (does not have a body) public abstract void sound(); // Regular method public void sleep() { System.out.println("Animal is sleeping"); } } class Dog extends Animal { // Providing implementation for the abstract method public void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); // Output: Dog barks dog.sleep(); // Output: Animal is sleeping } }
  • Interface Example:

    interface Animal { public void sound(); } class Dog implements Animal { public void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); // Output: Dog barks } }

Summary:

  • Class: Blueprint of objects.
  • Object: Instance of a class.
  • Encapsulation: Bundling data and methods together and controlling access to them.
  • Inheritance: One class inherits properties and methods from another.
  • Polymorphism: One interface used for multiple methods (overloading, overriding).
  • Abstraction: Hiding details, showing only essential features.

Java OOP principles help build robust, maintainable, and scalable software systems by breaking complex problems into smaller, reusable components.