Grade X Unit 2- Class as the Basis of All Computation

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

CLASS

§The user defined data type which


contains data and its
associated functions encapsulated or
wrapped together is known as a class.
§It is also known as blueprint or
prototype from which objects are
created.
§The internal data of a class is known as
data member and the procedures are
known as member methods (or
functions)
Data (or Data members or
Attributes)
§The variables declared within the class
on which operations to be (are)
performed are known as data or data
members or attributes or fields.
§The data members (variables) must be
declared along with its data type.
§Example: int a,b; float x,y;
char ch; long m;
Note: Data members define state of a
class. (object)
Method (or Function
or Behaviour)
•A method is a collection of
statements that are grouped
together to perform an
operation.
§Example:
void display()
{
a++;
b=a*10;
System.out.println(a+” ”+b);
}
Note: Methods
define behaviour of a class.
Example: Data members
and Member methods

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)

Difference between Class


variable and Instance
variable
Scope of Variables
vThe access range of variables within
the class is known as scope of
variables.
vRules
1.The instance variable is
accessible inside all instance functions
within the class.
2. The static or class variable is
accessible in all the functions (static or
instance functions) within the class.
3. The local variable is
accessible within that function only
where it is declared that is within the
opening and closing braces of that
function.
Example: Scope of
Variables

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 data types within


the class
ØA class can contain many type of
variables as its members.
ØThe variables declared using byte,
short, int, long, float, double, char
and boolean are called primitive type
members of a class.
Note: The block which contains primitive
data types as its members is known as
composite data type.
Class: Composite data
type
A class is known as composite data
type because a class contains many
primitive data types as its members.

Data Types in Java


•Data types represent the types of
different values to be stored in the
variable.
•In Java there are two types of
data types:
ØPrimitive
ØNon-primitive

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.

You might also like