A Java Program Must Be Encapsulated In A Class That Defines The State And
Behavior Of Basic Program Components Known As Objects.
Classes Create Objects And Objects Use Methods To Communicate Between Them.
Classes Contain A Group Of Logically Related Data Items (Called As Fields) And
Functions (Called As Methods) That Operate On Them. Calling A Specific Method In
An Object Is Described As Sending The Object A Message.
CLASSES AND OBJECTS
2
A class is a user defined data type with a template that serves to define its
properties. Objects are nothing but instances of classes. They can be created using
declarations. The basic form of a class definition is
class classname [extends super classname]
{
[Variables declaration;]
[Methods declaration;]
}
Everything inside square bracket is optional. Classname and super classname are any valid
Java identifiers. The keyword extends indicates that the properties of the super class are
extended to the sub class. A class can have emptybody.
DEFINING A CLASS
class Empty
{
}
By convention, all class names start
with upper case letter
alled as instance variables and they are created whenever an object of the class is instantiated. Insta
class Rectangle
{ int len, wid;
void getData (int x, int y)
{ len =x; wid=y; }
int recArea ()
{ int area = len * wid;
return (area); }
}
Adding variables
Adding methods (Defining a method)
A class can have any no. of methods called as instance methods.
Methods are used to manipulate the data contained in the class.
Method definitions should be present inside the class
immediately after the instance variables declarations. A method
is of the form
type method name (parameter list)
{
method body;
}
CREATING OBJECTS (INITIALIZING AN OBJECT)
An object in Java is a block of memory having enough space to store all instance
variables. Objects can be created using the new operator which creates an object of
the specified class and returns a reference to that object .
The general form for creating an object is
classname object;
object = new classname( );
The declaration and instantiation can be combined into a single statement
classname object = new classname( ) ;
Rectangle rect1 = new Rectangle( );
Any no. of objects can be created to a class
Rectangle rect1= new Rectangle();
Rectangle rect2= new Rectangle();
Eg; - Rectangle rect1;
rect1 = new Rectangle( );
Any no. of references can also be created to
an object
Rectangle rect1 = new rectangle();
Rectangle rect2 = rect1;
ACCESSING CLASS MEMBERS
• Its general from is
objectname.variablename
objectname.methodname (parameter list)
where
 objectname is the name of the object
 variable name is the name of the instance variable
 methodname is the name of the method
 parameter list is a list of actual values separated
by commas.
Each object has its own set of variables and methods. We cannot access these variables
and methods directly from outside the class. For this purpose, the dot operator is used.
Its general from is
Eg;-
rl.len =15;
rl.wid =10;
Thus, we can assign values to instance variables
We can also assign values to variables using methods declared inside the
Rectangle r1 = new Rectangle( );
rl.getData(15,10);
5
class Rectangle
{
int length, width;
void getData(int x, int y)
{
length=x;
width=y;
}
int rectArea ( )
{
int area = length * width;
return (area);
}
EXAMPLE
class Rectarea
{
public static void main (String args[])
{
int area1, area2;
Rectangle rect1 = new Rectangle( );
Rectangle rect2 = new Rectangle( );
rect1.length =15;
rect1.width =10;
area1 = rect1.length * rect1.width;
rect2.getData (20, 12);
area2 = rect2.rectArea( );
System.out.println (“Area of rectangle 1 = “ + area1);
System.out.println (“Area of rectangle 2 =” + area2);
}
}
6

1707325642974_Classes fffand objects.pptx

  • 1.
    A Java ProgramMust Be Encapsulated In A Class That Defines The State And Behavior Of Basic Program Components Known As Objects. Classes Create Objects And Objects Use Methods To Communicate Between Them. Classes Contain A Group Of Logically Related Data Items (Called As Fields) And Functions (Called As Methods) That Operate On Them. Calling A Specific Method In An Object Is Described As Sending The Object A Message. CLASSES AND OBJECTS
  • 2.
    2 A class isa user defined data type with a template that serves to define its properties. Objects are nothing but instances of classes. They can be created using declarations. The basic form of a class definition is class classname [extends super classname] { [Variables declaration;] [Methods declaration;] } Everything inside square bracket is optional. Classname and super classname are any valid Java identifiers. The keyword extends indicates that the properties of the super class are extended to the sub class. A class can have emptybody. DEFINING A CLASS class Empty { } By convention, all class names start with upper case letter
  • 3.
    alled as instancevariables and they are created whenever an object of the class is instantiated. Insta class Rectangle { int len, wid; void getData (int x, int y) { len =x; wid=y; } int recArea () { int area = len * wid; return (area); } } Adding variables Adding methods (Defining a method) A class can have any no. of methods called as instance methods. Methods are used to manipulate the data contained in the class. Method definitions should be present inside the class immediately after the instance variables declarations. A method is of the form type method name (parameter list) { method body; }
  • 4.
    CREATING OBJECTS (INITIALIZINGAN OBJECT) An object in Java is a block of memory having enough space to store all instance variables. Objects can be created using the new operator which creates an object of the specified class and returns a reference to that object . The general form for creating an object is classname object; object = new classname( ); The declaration and instantiation can be combined into a single statement classname object = new classname( ) ; Rectangle rect1 = new Rectangle( ); Any no. of objects can be created to a class Rectangle rect1= new Rectangle(); Rectangle rect2= new Rectangle(); Eg; - Rectangle rect1; rect1 = new Rectangle( ); Any no. of references can also be created to an object Rectangle rect1 = new rectangle(); Rectangle rect2 = rect1;
  • 5.
    ACCESSING CLASS MEMBERS •Its general from is objectname.variablename objectname.methodname (parameter list) where  objectname is the name of the object  variable name is the name of the instance variable  methodname is the name of the method  parameter list is a list of actual values separated by commas. Each object has its own set of variables and methods. We cannot access these variables and methods directly from outside the class. For this purpose, the dot operator is used. Its general from is Eg;- rl.len =15; rl.wid =10; Thus, we can assign values to instance variables We can also assign values to variables using methods declared inside the Rectangle r1 = new Rectangle( ); rl.getData(15,10); 5
  • 6.
    class Rectangle { int length,width; void getData(int x, int y) { length=x; width=y; } int rectArea ( ) { int area = length * width; return (area); } EXAMPLE class Rectarea { public static void main (String args[]) { int area1, area2; Rectangle rect1 = new Rectangle( ); Rectangle rect2 = new Rectangle( ); rect1.length =15; rect1.width =10; area1 = rect1.length * rect1.width; rect2.getData (20, 12); area2 = rect2.rectArea( ); System.out.println (“Area of rectangle 1 = “ + area1); System.out.println (“Area of rectangle 2 =” + area2); } } 6