6. Exception Handling (try-catch)

6.4. Object

What is an Object?

An object is a specific instance of a class, created from the class blueprint. It holds actual data and can perform the actions defined in the class.

Key Points

Objects are like individual items made from the class template.

Each object can have different data but follows the same structure.

Example: A person named Alice is an object of the Person class.

Examples

Java:

Person person = new Person(); // Creates an object

Python:

person = Person() # Creates an object

3. Attribute

An attribute is a piece of data (like a variable) that belongs to a class or object. It describes the object’s properties, such as its name or age.

Key Points

Attributes store information specific to an object.

In Java, attributes have explicit data types; in Python, types are inferred.

Example: A Person class might have attributes like name and age.

Examples

Java:

class Person {

String name; // Attribute

int age; // Attribute

}

Python:

class Person:

    def __init__(self, name, age):

        self.name = name # Attribute

self.age = age # Attribute