java

Java OOP


What is OOP?

OOP stands for Object-Oriented Programming. It is a programming paradigm that uses objects and classes for organizing code. The main idea behind OOP is to model software components (objects) based on real-world entities and the interactions between them.

 

Here are the key concepts of Java OOP

1. Classes and Objects:

Class: A class is a blueprint or template for creating objects. It defines the attributes (fields) and behaviors (methods) that an object can have.
Object: An object is an instance of a class or real world entity that have state and behavior.

 

Syntax

class Car {
    // Fields (attributes)
    String model;
    int year;

    // Methods (behaviors)
    void start() {
        System.out.println("Car is starting...");
    }
}

// Creating an object
Car myCar = new Car();

 

2. Encapsulation:

Encapsulation is the bundling of data (attributes or fields) and methods (functions or procedures) that operate on that data into a single unit called a class.
It restricts access to some of the object's components, emphasizing the idea of hiding the internal workings of an object and only exposing what is necessary.

Note:- The main idea behind encapsulation is to hide the internal details of an object and control access to its state. Making data members private is a way to achieve this.

 

Syntax

public class Book {
    // Private fields
    private String title;
    private String author;
    private int pages;

    // Public constructor
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }

    // Public method to display book details
    public void displayBookDetails() {
    }

    public static void main(String[] args) {
        // Creating a Book object
        Book book = new Book("The Catcher in the Rye", "J.D. Salinger", 224);
        book.displayBookDetails();  // By this way we can call the method
    }
}

 

3. Inheritance:

Inheritance allows a class (subclass/derived class) to inherit the properties and methods of another class (superclass/base class). 

Note :- It promotes code reusability.

 

Syntax

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

class Dog extends Animal {
    void bark() {
    }
}
class A {
    public static void main(String[] args) {
        // Create a Dog object
        Dog myDog = new Dog();

        // Accessing properties and methods from the subclass (Dog)
        myDog.eat();  // Inherited from Animal class
        myDog.bark(); // Specific to Dog class
    }
}

 

4. Polymorphism:

Polymorphism allows objects of different classes to be treated as objects of a common base class. It can be achieved through method overloading and method overriding.

Note :- Polymorphism means "Many Forms".

 

Syntax

class Animal {
    void makeSound() {
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
    }
}

class Cat extends Animal {
    @Override
    void makeSound() {
    }
}

public class PolymorphismExample {
    
    public static void main(String[] args) {
        // Creating objects of different types
    
        Dog myDog = new Dog();
        Cat myCat = new Cat();

        // Demonstrating polymorphism
       myDog.makeSound()       // Calls makeSound() from Dog class
       myCat.makeSound()         // Calls makeSound() from Cat class
    }
}

 

5. Abstraction:

Abstraction involves hiding the complex implementation details and showing only the essential features of an object.
Abstract classes and interfaces are used to achieve abstraction in Java.

 

Syntax

// Example of abstraction in Java using abstract class
abstract class Shape {
    // Abstract method (to be implemented by subclasses)
    abstract void draw();
}

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

class Square extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a square");
    }
}