0% found this document useful (0 votes)
10 views4 pages

Ch.1 Unit.2

The document introduces the concepts of classes and objects in programming, comparing them to real-world categories and items. It explains that a class serves as a blueprint for creating objects, which are instances that possess characteristics and behaviors defined by the class. Additionally, it outlines the structure of class declarations and provides examples of how to create and utilize classes and objects in programming.

Uploaded by

Aryan Kadbane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Ch.1 Unit.2

The document introduces the concepts of classes and objects in programming, comparing them to real-world categories and items. It explains that a class serves as a blueprint for creating objects, which are instances that possess characteristics and behaviors defined by the class. Additionally, it outlines the structure of class declarations and provides examples of how to create and utilize classes and objects in programming.

Uploaded by

Aryan Kadbane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit-II

Elementary Concept of Objects and Classes

Learning Scope
" Introduction Real world obiects and software objects " Characteristics and
behaviour of real world objects and software objects "Class is an object factory
" Object is an instance of a class " Class is a user defined data type

Introduction
In real world, the class is a set or category of
things
sharing common_characteristics or qualities. Each Class
item belonging to a specific category possesses the Object,
characteristics and behaviour described within it.
If we consider furniture' as a category then different types of furnitures such as table,
chair, bench, etc. can be referred to as the items of category furniture. In software
terms, category and items are considered to be class and objects respectively.
Hence, class is a blue print/template/prototype that describes the characteristics and
behaviour for the similar types of objects. Objects are the entities which possess different
characteristics and common behaviour described within the class.
Let us consider an example of real world class to
understand the way different
characteristics and common behaviour are embedded in its objects.

La
Class TvSet SONY

Object 1
Object 2
Characteristics: Characteristics:
Size Size Characteristics:
Size
Number of channels Number of channels
Number of channels
Price Price
Price

Behaviour:
Common Behaviour:
Viewing TV programs
Playing games
Viewing TV programs
Viewing movies Playing games
Viewing movies

10 |Un
(renting an Object of a Class
object of a class can be
An created by using the following syntax:
<Cluss name> <Obiec name> =
new Class
With reference to the above illustration, some name>():
described under class named as TvSet'. The characteristics
objects of
and behaviour of TV
cRated shown -below: class TvSet' can be
T:Set SONY =new TSer(0:
T:Set LG = nes ISet):
The moment the objects are created, they will implicitly possess the
dhehaviour described within the class. Here, it is to be noted that characteristics
the objects SONY
ad LG will contain different characteristics (Size. Number of channels and
Price) but
they will possess common behaviour such as Viewing TV programs', Playing games
ond Vievwing movies". In this way, we can say that the class and objects are inter-related
to each other.
When class and objects are used in programming, they are known as software clasS
and objects. The description of class and declaration of the objects will take place in
the same way as the real world class and objects take (already discussed above).

Note:
A class declaration includes the following components:
Access Specifier: Aclass can have only access specifîer (i.e. public) whiçh is a default
specifier.
Class name: The class name must be meaningful with initial letter in uppercase.
Body: The set of statements comprising aclass must be enclosed under curly braces, { }.

used
Let us take an example to understand as how software class and objects will be
for programming purpose.
class Rectangle Objects

I/Data Members Object 1 Object 2


length
breadth Fig_1 Fig 2
I/Member Methods |length= 36 length= 42
breadth= 16 breadth= 20
input()
calculate()
input)
calculate()
display()
display()

shown above, the class 'Rectangle' is designed with the data members
In the structure (input(), calculate() display(). The objects
and
(length ad breadth) and member functions
of the class can be created as shown below:
Rectangle Fig 1 = new Rectangle():
Rectangle Fig 2= new Rectangle():
Revision of Class IX Syllabus | 11
The structure shown above, stales when the objects Fig_l and Fig_2 are created then
cach object will automatically possess the data members and member functions described
within the class Rectangle'. The allocaion of data members pertaining to cach objct
can be different but the functions to be carried on them will be common.
Hence,
Fig_I.inpu() : will accept length and breadth of object Fig_I.
Fig_l.calculate() : will perform calculations by using length and breadth of object
Fig_I.
Fig_I.display() : will display the result of calculation of object Fig_I.
Similarly, you can show the above tasks for the object Fig _2.
With reference to creating objects of a class 'Rectangle', the complete program i
designed on BlueJ platform:
a program to calculate area of rectangle using class and function
import java.util.*:
class Rectangle
int l,b,ar;
public void Input()

Scanner in = new Scanner(System.in);


System.out.println("Enter length of rectangle:");
l=in.nextlnt();
System.out.println("Enter breadth of rectangle:");
b=in.nextInt);
public void Calculate()

ar=|*b:

public void Display()

System.out.println("Length of rectangle ="+);


System.out.println("Breadth of rectangle= "+b);
System.out.println("Area of rectangle ="+ar);
public static void main(String args[)

Rectangle ob=new Rectangle();


ob.Input);
ob.Calculate():
ob.Display0:

12 | Understanding Computer Applications with BlueJ--X


The output of
the above program is shown below:
BlueJ Terminal Window Computer 10
Options
Enter length of rectangle:
36
Enter Breadth of rectangle:
16
Length of rectangle = 36
Breadth of rectangle = 16
Area of rectangle = 576

Real World vs Software Class and Objects


From the real world and software examples of class and objects shown above, it is
evident that the characteristics and behaviour of real world objects are treated as data
members and member methods of software objects respectively.
Characteristics Data Members
Size length (1)
Number of channels breadth (b)
Price area (ar)

Behaviour Functions
Viewing TV Programs input()
Playing Games calculate()
Viewing movies display()

Real world Software world

the same class will possess different data


It must be noted that a number of objects of
members but common methods or functions.

Features of a Class
object factory: A class is used to create similar objects that possess
Class is an Hence, class is called an object
differentcharacteristics and common behaviour.
factory.
class: The data members declared within a class are also
Object is an instance of a of a class is created, it includes
instance
When an object
known as instance variables. This is the reason that object is called as
an
the class.
variable described within
instance of a class. becomes a data type
data type: When user creates a class, it
Class is a user defined
referred to as a user defined data type. This data type
is
Tor his program. Thus, class types such as int, float, char, etc. Hence, it is also said
includes different primitive
to be Composite Data Type.

You might also like