Ch 7: Classes 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)
{
.
.
.
}
}
Amit Kr. Trivedi
7.3 Types of Variables:
• Instance variables: Global variables declared outside all the functions.
• Class variables: Global variables, preceded with ‘static’ keyword, declared outside all
the functions.
• Argument variables: Function’s parameter list.
• Local variables: Declared inside the body of a function.
e.g.
public class variables
{
int a; // instance(object) variable
static int b; // Class variable
public static void main (int x)
{ // Argument variable
int y; // Local Variable
.
.
}
}
7.4 Access Specifiers: These determines the accessibility of the members of a class.
Same package Other Packages
Access
Specifier Derived Other Derived Other
Same class
Classes Classes Classes Classes
public Accessible Accessible Accessible Accessible Accessible
Not
protected Accessible Accessible Accessible Accessible
Accessible
Default Not Not
Accessible Accessible Accessible
(friendly) Accessible Accessible
Not Not Not Not
private Accessible
Accessible Accessible Accessible Accessible
Amit Kr. Trivedi
7.5 Instantiation: Creating an object with the help of ‘new’ operator is called instantiating an
object.
The General Syntax to instantiating an object is as:
<Class-name> <Object-name> = new <Class-name> ( Argument(s));
e.g. Scanner sc = new Scanner (System.in);
7.6 Static members and Non-static members
Class(Static) Members Instance(object) members
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.
7. To declare a member as the class 7. It doesn’t required any modifier to
member, ‘static’ modifier is required. declare an instance member.
e.g. static int a; e.g. int a;
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.
Amit Kr. Trivedi
Sol.
import java.util.*;
class Merger
{
long n1,n2,mergNum;
//default Constructor
public Merger()
{
n1=0;
n2=0;
mergNum=0;
}
//To accept the values of n1 and n2
void readNum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Two numbers");
n1=sc.nextLong();
n2=sc.nextLong();
}
//To concatenate two numbers
void JoinNum()
{
Amit Kr. Trivedi
long c=n2;
int count=0;
//to count no. of digits
while(c!=0)
{
count++;
c/=10;
}
mergNum=n1*(int)Math.pow(10,count) + n2;
}
//To display
void show()
{
System.out.println("Original numbers are: \n"+n1+"\n"+n2);
System.out.println("Merged number ="+mergNum);
}
// To execute the program
public static void main()
{
Merger ob= new Merger();
ob.readNum();
ob.JoinNum();
ob.show();
}
}
Amit Kr. Trivedi
Amit Kr. Trivedi