Professional Documents
Culture Documents
Grade X Unit 2- Class as the Basis of All Computation
Grade X Unit 2- Class as the Basis of All Computation
Grade X Unit 2- Class as the Basis of All Computation
OBJECT
An object is a unique entity, which
contains data and functions.
Characteristics of an
Object
An object consists of the following:
1.State: Represented by attributes of
an object. It also reflects the properties
of an object.
2.Behaviour: Represented
by methods of an object. It also reflects
the response of an object with other
objects.
3.Identity: Gives a unique name to an
object and enables one object to
interact with other objects.
Example: Object
Message Passing
Software Objects can interact and
communicate with each other through
parameters (messages). This process is known
as Message passing.
Example : Teacher interact with student.
Student read from the book.
DECLARATION OF
VARIABLES
•Declaring a variable simply means
specifying what type of value is to be
stored in a variable.
Example: int age;
•The syntax is
datatype variable_name;
•A variable has three properties—
a memory location to store the value,
the type of data stored in it, and
the name used to access the value.
INITIALISATION OF
VARIABLES
üInitialisation is the assignment of a value to
a variable.
üWhen declaring a variable, it allocates
storage for the variable defined.
üInitialisation, on the other hand, is the
specification of the initial value to be stored
in an object.
Example: Static, Instance
and Local Variables
Static Method (or Static
Function)
vThe method which begins with static
keyword is known as static method.
vThese functions uses only static
variables.
vExample: public static void show( )
or
static void show( ) //use of public is
optional
Instance Method (or
Instance Function)
vThe method which uses both static
(or class) and instance variables is
known as instance method.
vExample: public void show( )
or
void show( ) //use of public is
optional
Example
Example
Example: Accessing static members of class
(Test) from another class (static_members)
Example: Scope of
Variables
Object as instance of a
class
A class is a template or blueprint
from which objects are created.
So object is the instance of a
class.
Instantiation
Instantiation is the process of creating
an instance or object of a class. The
new keyword is used to instantiate a
class.
Syntax: To create object of a class
class_name object_name=new cl
ass_name( );
Example:
Object Factory
Each object belonging to a specific class
possesses the data and methods
defined within the class. It produces
objects of similar type. Hence a class is
termed as Object Factory.
Example:
Student s1=new Student();
Student s2=new Student();
Student s3=new Student();
Student s4=new Student();
Accessing members of a
class
Syntax: To access data members of a
class
object_name . data_member_na
me;
Syntax: To access member functions of
a class
object_name . function_name(param
eter/argument list);
Example: Accessing
members of a class
Q1. In the program given below, state the
name of following :
(i)class variable (ii) instance variable
(iii) local variable
class Test
{
static int x=7;
int y=2;
public static void main()
{
int a=6;
Test t1=new Test();
System.out.println(x);
System.out.println(t1.y);
System.out.println(a);
}
}
Ans. class variable: x
instance variable: y
local variable: a
Q2. Consider the following class :
class Variable
{
static int x=7,y=9;
int a=2,b=8;
public static void main()
}
(i)Name the variables for which each
object of the class will have its own
distinct copy.
(ii)Name the variables that are common to
all objects of the class.
Ans. (i) a and b
(ii) x and y
Q3. A class Cricket contains a function
void display(). Write a valid statement to
create object and call (access) the
function.
Ans. Cricket obj=new Cricket();
obj.display();
Primitive/ Fundamental/
Basic/ Intrinsic Data Types
•These are the pre
defined datatypes already present in
the java language.
•Represent numbers,
characters, boolean values.
•Integers: byte, short, int, and long
•Real numbers: float and double
•Characters: char
•Boolean: boolean
Non-Primitive/ User
defined / Derived/
Composite/ Reference Data
Types
•These datatypes are created from
the combination of
primitive datatypes by the user.
•Example
Arrays etc.
Program 2: Functions-2
Define a class Character with the following
description:
Instance variables/ data members:
char ch- to store the character
Member methods:
void input()- to input and store the character
void check()- to check and display whether the
entered character is a lowercase or uppercase letter
or not a letter of the English alphabet.
Write a main method to create an object of the
class and call the above methods.