Page 30 - CA_Blue( J )_Class10
P. 30
Bus(String color, String model)
{
this.color = color;
this.model = model;
}
// Method (behaviour)
void drive()
{
System.out.println("The bus is of " + color + " and made by "+ model + "
company. " );
}
}
// Using the class to create objects
class Object_Factory
{
public static void main()
{
// Creating objects (instances) of the Bus class
Bus Bus1 = new Bus("Red", "Tata Motors");
Bus Bus2 = new Bus("Multicolor", "Ashok Leyland");
// Calling methods on the objects
Bus1.drive();
Bus2.drive();
}
}
Output:
The bus is of Red and made by Tata Motors company.
The bus is of Multicolor and made by Ashok Leyland company.
Note: In this example, the Bus class serves as an object factory. It defines what a bus object should look
like and how it should behave. The Object_Factory class then uses this blueprint to create specific car
objects, demonstrating the class's role as an object factory.
2.3.2 Object is an Instance of a Class
While defining a class in Java, it does not occupy any space in memory. But, as soon as an object is created, it acquires
memory space in the computer. An object is the physical existence of a class. While creating an object of a class, we are
basically defining a variable that has the size of the total bytes of all the data members. Hence, an object is also called
an instance of a class as it has the same properties as its class.
• Class Definition: The Bus class defines the structure and behaviour of a person with attributes name and age, and
a method drive().
• Object Creation: In the Object_Factory class, two objects (Bus1 and Bus2) are created using the Bus class.
2828 Touchpad Computer Applications-X

