What is Class in OOPs


In object-oriented programming (OOP), a class is a template or blueprint for creating objects. An object is an instance of a class, and it has its own state (data) and behavior (methods).

A class defines the characteristics and behavior of a group of objects. It specifies the data that the objects will contain (attributes or instance variables) and the operations that the objects will be able to perform (methods or functions).

Here is an example of a simple class in Python:

class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed
    def bark(self):
        return "Woof!"

In this example, the Dog class has two attributes, name and breed, and one method, bark. The __init__ method is a special method in Python called a constructor, which is called when you create a new instance of the class. It initializes the attributes of the object with the values passed as arguments.

To create an instance of the Dog class, you can use the following syntax:

dog1 = Dog("Fido", "Labrador")
dog2 = Dog("Buddy", "Beagle")

You can access the attributes and call the methods of an object using the dot notation:

print(dog1.name)  # "Fido"
print(dog1.breed)  # "Labrador"
print(dog1.bark())  #

Leave a Reply