Classes&Objects
Classes&Objects
Java Classes
A class in Java is a set of objects which shares common characteristics/ behavior and common
properties/ attributes. It is a user-defined blueprint or prototype from which objects are created.
For example, Student is a class while a particular student named Ravi is an object.
• Data member
• Method
• Constructor
• Nested Class
• Interface
Syntax:
access_modifier class <class_name>
{
data member;
method;
constructor;
nested class;
interface;
}
Java Objects
Objects are the instances of a class that are created to use the attributes and methods of a class.
// Java Program for class and Object example
class Room
// data member (also instance variable)
int length;
int width;
public static void main(String args[])
{
Room r1 = new Room(); // creating an object of Student
r1.length=10;
r1.width=3;
System.out.println(“Length:”+ r1.length);
System.out.println(“Width:”+ r1.width);
}}
Output:
Length:10
width:3