Page 31 - CA_Blue( J )_Class10
P. 31
• Instance State: Each object has its own state, stored in the name and age fields. Bus1 and Bus2 have different
states, demonstrating that they are separate instances of the Bus class.
• Method Invocation: The drive() method is called on each object, showing that objects can use the methods defined
by their class, and the output reflects their individual states.
This example illustrates how objects are instances of a class, each with their own unique state and behavior defined
by the class.
2.3.3 Class is a user-defined Data Type
A user-defined data type is a derived data type from some existing data types. A Java program is made up of classes
and each class is made up of some attributes (data members) which are predefined data types. While creating an
object, we are essentially defining a variable whose size is equal to the total size of all the predefined data types
included in the class. Thus, we can say a class is a user-defined data type.
For example:
class Story_Book // Define a class called Story_Book
{
// Fields (attributes)
String title;
String author;
int pages;
// Constructor
Story_Book(String t1, String a1, int p1)
{
title = t1;
author = a1;
pages = p1;
}
// Method (behaviour)
void printDetails()
{
System.out.println("Title: " + title + "\nAuthor: " + author + "\nPages: " + pages);
}
}
// Using the class as a user-defined data type
class Orange
{
public static void main()
{
// Creating an object (instance) of the Book class
Story_Book my_Book = new Story_Book("The Story of Doctor Dolittle", "Hugh
Lofting", 350);
// Accessing the object's attributes and methods
my_Book.printDetails();
}
}
29
Elementary Concept of Objects and Classes 29

