Menu
Topics Index
...
`


Classes >
Siva Nookala - 22 Mar 2016
Here are few points which we need to understand about classes and objects.
  • A class is like a blue print and we can create as many objects using that class.
  • All the objects of one class will have same properties and they will be copied from that class
  • We can create multiple objects of a class and multiple objects can be of same class
  • Every object should belong to a class, since we can not create objects with out a class
  • A class is also referred as type and an object is also referred as instance.

To understand the difference between a class and objects, let us look at the following examples.
Class Object Properties Description
Brick Mold Bricks Width, Breadth, Height Using one brick mold we can create as many bricks. Every brick has the same width, breadth and height as the mold.
Refrigerator Ice Tray Ice cubes Width, Breadth, Height Similar to brick mold, the size of the ice cube is dependent on the tray
Original Examination Form Photo Copied Examination Forms Fields like Name, School, Percentage, Subjects and any instructions and guidelines for filling the form We can take as many copies of Examination forms using the original and every copy has same fields, instructions and guidelines as the original form.
House Plan Houses Number of bedrooms, kitchen size, toilets, hall size, appliances location etc We can construct multiple houses using a plan and every house will have the same number of bedrooms, kitchen size etc.
Student Mahesh, NTR, Prabhas, Arjun Name, Marks, Section, Address, Mobile Number Every object created using the Student class will have the same properties.
Compared to the creation of primitive types, creating an instance (or object) of Student class (or type) is different. We have to use the new keyword for creating an object of non-primitive data type or user defined data type. For e.g., in primitive types, the moment a variable is declared, the space required for that variable is also allocated.
int a = 5;
This declares the variable a and allocates 4 bytes for that integer. But with classes (or non-primitive data types), the allocation of the space required for that object has to be done using new keyword as shown below. If the class Student is declared like this
class Student
{
    String name;
    int marks;
    char section;
}
we can create object for student Mahesh like this.
Student mahesh = new Student();
Similarly, we can also create student objects like ntr, prabhas and arjun as shown below.
Student ntr = new Student();
Student prabhas = new Student();
Student arjun = new Student();
Above, the right side of equals shows how to create objects and ntr, prabhas and arjun are called references. We will discuss more about references in Java Objects References. Classes-objects

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.