close menu
Concepts-of-Classes-and-Objects Concepts of Classes and Objects

Concepts of Classes and Objects

17 February 2025

 Concepts of Classes and Objects


Concepts of Classes and Objects
- Creating classes and objects

**Concepts of Classes ans Objects - Creating Classes and Objects
Object-Oriented Programming(OOP) revolves around classes and objects. A class acts as a blueprint, While objects are instance of a class containing data and behaviors.

**Defining a Class
A class encapsulates attributes and methods.
class Car{
        String brand;
        int speed;
        void displayInfo(){
            System.ot.println("Brand:"+brand);
            System.out.println("Speed:"+speed+"km/h");
            }
        }

Creating an Object
An object is an instance of a class.

Car myCar = new Car();
myCar.brand = "Toyota";
myCar.speed = 120;
myCar.displayInfo();

**Constructors
A constructor initializes an object.

class Car{
    String brand;
    int speed;
    Car(String b, int s){
        brand = b;
        speed = s;
    }
}

**Encapsulation
Encapsulation restricts direct access to data using private access modifiers and getter/setter methods.

class Car {
   private String brand;
   private int speed;
   void setBrand(String b) { brand = b; }
   String getBrand() { return brand; }
}

**Conclusion
Classes and objects are core OOP concepts, enabling efficient software development through encapsulation and constructors.

Whatsapp logo