0% found this document useful (0 votes)
233 views3 pages

CH 2 Class As Basis of All Computation

A class is a template that defines the characteristics of an object including its state, behavior, and identity. An object is an instance of a class that allocates memory at runtime. The example creates a Student1 class and then uses the new keyword to create two Student1 objects that can store student ID and name data.

Uploaded by

shubhali vankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
233 views3 pages

CH 2 Class As Basis of All Computation

A class is a template that defines the characteristics of an object including its state, behavior, and identity. An object is an instance of a class that allocates memory at runtime. The example creates a Student1 class and then uses the new keyword to create two Student1 objects that can store student ID and name data.

Uploaded by

shubhali vankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ch 2 Class as Basis of All Computation

Object: An entity that has state and behavior is known as an object.


e.g., chair, bike, marker, pen, table, car, etc.

An object has three characteristics:


• State: represents the data (value) of an object.
• Behavior: represents the behavior (functionality) of an object
such as deposit, withdraw, etc.
• Identity: An object identity is typically implemented via a
unique ID. The value of the ID is not visible to the external user.
However, it is used internally by the JVM to identify each object
uniquely.
• For Example, Pen is an object.
Its name is Reynolds;color is white, known as its state. It is used to
write, so writing is its behavior.

Class
A class is a template or blueprint from which objects are created.
So, an object is the instance(result) of a class.
• Class is a logical entity whereas object is a physical entity
• An object is an instance of a class.
Types Of Access Specifiers :

In java we have four Access Specifiers and they are listed below.

1. public
2. private
3. protected
4. default(no specifier)

new keyword is used to allocate memory at runtime by creating objects.


// Example Program for creating class and object

class Student1{
int id;
String name;

public static void main(String args[]){


//Creating objects
Student1 s1=new Student1();
Student1 s2=new Student1();
//Initializing objects
s1.id=101;
s1.name="Sony";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}

You might also like