Ch 07Classes in Java
Ch 07Classes in Java
7.1:Object: An identifiable entity that has characteristics and behavior is called an object.
Features of an Object:
1. Identity: uniquely identify an object.
e.g. Nikunj Nikunj Nikunj
2. Attribute: Characteristics of
an object.
e.g. Class, Roll No., age etc.
3. State: Depicted through the
values of its attributes.
e.g. 11B,12B,25 etc.
4. Behavior: Functionalities associated with an object.
e.g. Promotion etc.
7.2 Class: A collection of objects sharing common properties and behaviours.
or
A class is a way to wrap data (characteristics) and its associated functions (behaviours) into a
single unit.
e.g.
public class xyz
{
int a; // Data member (represent characteristics)
public void func() // Member function (represents behavior)
{
.
.
.
}
}
}
}
7.4 Access Specifiers: These determines the accessibility of the members of a class.
1. The static member allocates a memory 1. The non-static member creates its
location only once and same memory copies on the basis of number of objects
location is shared by all objects of the created for the class .
class.
2. Static members can be accessed 2. Instance members are accessible
without instance. Name of the class is used through an object only.
to access the static members.
Note: A static members cannot access a non-static members of the class directly.
An object of the class is required to access non-static member by a static member.
public class XYZ
{
int a; // instance(object) variable
static int b; // Class variable
Amit Kr. Trivedi
public void function()
{
int s=a+b;
System.out.println(“sum= ” +s);
}
public static void main ()
{ XYZ ob = new XYZ ();
b=15; //Accessing a static variable
ob.a = 10; //Accessing an instance variable
ob.function(); //Calling an instance method
} }
Q.
//default Constructor
public Merger()
{
n1=0;
n2=0;
mergNum=0;
}