DEV Community

VIDHYA VARSHINI
VIDHYA VARSHINI

Posted on

OOP's concept in Java

What is OOP's: OOP stands for Object Oriented Programming.
Primary of OOP's - Object
Primary of POP - procedure(steps)
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

Key concepts of OOP's :

1. Class: Class is a blueprint or template for creating objects. This defines properties and behavior(methods).

How to create class in Java:
In Java, a class is created using the class keyword, followed by the class name and a pair of curly braces {} that contain the fields (variables) and methods (actions) of the class.
Class name should always starts with capital letter.
No space is allowed between the variables.
No special characters allowed(except $ and _).

Ex:

class Car {
    String color;
    int speed;

    void drive() {
        System.out.println("Car is driving at speed: " + speed);
    }
}

Enter fullscreen mode Exit fullscreen mode

2. Object Object is a combination of state and behavior. This occupies space to perform some action. It is an instance of a class.

Ex:

class Main {
    public static void main(String[] args) {
        Car myCar = new Car();  
        myCar.color = "Red";
        myCar.speed = 100;
        myCar.drive();  
    }
}

Enter fullscreen mode Exit fullscreen mode

3. Inheritance: Inheritance allows one class to acquire the properties and methods of another class. This promotes code reusability.

4. Encapsulation: This is defined as the process of wrapping data and the methods into a single unit, typically a class. It is the mechanism that binds together the code and the data.

5. Abstraction: This is the process of hiding the implementation details and only showing the essential details or features to the user. It allows to focus on what an object does rather than how it does it.

6. Polymorphism: This means having many forms in which one function behaves differently based on the object or data type. In Java, polymorphism allows the same method or object to behave differently based on the context, specially on the project's actual runtime class.

Top comments (0)