Docsity

Introduction to Objects and Classes in Object-Oriented Programming, Study notes of Computer science

Toggle sidebar
bg1
Elementary
Concept
of
Objects
and
Classes
Learning
Scope
Introduction,
objects;
real
world
objects
and
software
objects,
Characteristics
and
behaviour
of
real
world
objects
and
software
objects,
Class
as
a
specification
of
objects,
creating
objects
of
a
class,
message
passing,
Important
terms
related
to
class:
class
as
an
object
factory,
class
as a user defined function, declaring states and methods
of
a class, object
as
an
instance
of
a
class,
differences
between
class
and
object.
INTRODUCTION
In
the
previous
chapter,
you
have
learned
about
Object
Oriented
Programming
(OOP)
and
its
principles
such
as
Data
abstraction,
Encapsulation,
Polymorphism
and
Inheritance.
An
Object
Oriented
Programming
(OOP)
language
not
only
promotes these features but
also
correlates
two
more
basic
units
(i.e.
Objects
and Classes).
In
this chapter,
we
are
going
to
discuss
the
features
or
properties
of
objects
and classes applicable
to
real world
as
well
as
to
software world.
OBJECTS
An
object
is
a
fundamental
unit
of
Object
Oriented
Programming
and
represents
the
real
life
entities.
In
Object
Oriented
Programming
(OOP),
the
attempts
are
made
to
break
a
task
into
some
components,
called
objects.
They
are
the
basic
elements
of
the
Object
Oriented
system
and
are
also
known
as
the
Entities.
Moreover,
a
set
of
related
objects
may
exchange
data
and
information
to
interact
with
each
other.
The
obijects
are
further
categorised
in
two
ways:
(a)
Real
World
Objects
(b)
Software
Objects
2
REAL
WORLD
OBJECTS
Real
world
objects
are
those
which
we
experience
or
use
in
our
day
to
day
lives.
A
specific
item
can
be
defined
as
a
real
world
object
if
it
possesses
the
following
features:
" It is visible to us.
"
It
has
a
definite
shape
and
size.
28
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Introduction to Objects and Classes in Object-Oriented Programming and more Study notes Computer science in PDF only on Docsity!

Elementary Concept of

Objects and Classes

Learning Scope

Introduction, objects; real world objects and software objects, Characteristics and behaviour

of real world objects and software objects, Class as a specification of objects, creating objects

of a class, message passing, Important terms related to class: class as an object factory, class

as a user defined function, declaring states and methods of a class, object as an instance of a (^) class, (^) differences between class (^) and (^) object.

INTRODUCTION

In the previous chapter, you have learned about Object Oriented Programming

(OOP) and its principles such as Data abstraction, Encapsulation, Polymorphism

and (^) Inheritance. An (^) Object (^) Oriented (^) Programming (OOP) (^) language not (^) only promotes these features but also correlates two more basic units (i.e. Objects and Classes). In this chapter, we are going to discuss the features or properties of objects and classes applicable to real world as well as to software world.

OBJECTS An (^) object is afundamental unit of (^) Object (^) Oriented (^) Programming and

represents

the real life (^) entities. In (^) Object (^) Oriented (^) Programming (OOP), the (^) attempts are

made to break a task into some components, called objects. They are the basic

elements of the Object Oriented system and are also known as the Entities.

Moreover, a set of related objects may exchange data and information to interact

with each other.

The obijects are further categorised in two ways:

(a) Real World Objects (b) Software Objects

REAL WORLD OBJECTS

Real world objects are those which we

experience or^ use in our day to day

lives. A^ specific item can be defined as a real world

object if it possesses the

following features: " (^) It (^) is (^) visible to us.

" It has a definite shape and

size.

28

" It can be brought into thoughts and figures.

Tiger

The characteristics are: Model

" App

Dial pad

The behaviours are:

"

Each real world object contains characteristics and behaviours. The characteristics basically comprises the parts of its body or specifications whereas, behaviour is the purpose of its use or its functions.

" It is used to talk.

For example,^ A^ mobile^ phone^ (cell^ phone)^ can^ be^ defined^ as^ a^ real^ world object that possesses some characteristics and behaviours as explained below.

Cycle (Real World Objects)

" It is used to browse the Internet. It is also used for net banking.

Table

Similarly, you^ can^ consider the^ Table'^ as^ an^ object.

Car

The characteristics of a table are:

" It has a plain glass top.

" It has four legs.

The behaviours^ of^ a^ table^ are:

" It is^ made^ up^ of^ wood.

" It^ is^ used^ to^ keep^ copies^ and^ books.

" It is used for study purposes.

9

It is used^ to^ keep^ glasses^ and^ plates.

8

Mobilephone

O7 65

YOU CANY OO IT E

Clock

The objects^ described^ above^ (like^ the^

table and

mobile) are^ non-living^ things.^ Broadly,^ speaking,

the living^ things^ are^ also considered^ as^ real world

objects. Under^ this^ situation,^ we^ may^ assume^ each^ living^

individual as an object

whose characteristics^ may be^ defined^ as^ their features and

their behaviour^ may be defined as the role or function they perform.

Elementary Concept^ of^ Objects^ and^

Classes 29

Let us have the comparative study of real world objects and software objects

with the help of a table given below:

Object

Real World (Book)

Object

Software

(Student)

Chapters

States or Characteristics

Pages

Topics

Name

Subjects

Marks

State or Characteristics

  1. Chapters
  2. Pages
    1. Topics

MESSAGE PASSING

A comparative study of characteristics and data members:

Real Worid Object (Book)

Behaviour

  1. Used to read
    1. Used to

Data Members

prepare notes

Real World Object (Book)

Function

A comparative study of behaviours and functions:

Data

Object A

Used to read Used to prepare notes

Message Passing

Accept( ) Display( )

Data Members

  1. Name
  2. Subjects
  3. MarkS

Behaviour

Software Object (Student)

Functions

  1. Accept( )
  2. Display( )

It is interesting to know that the objects can interact with each other. An object can pass information to another object and receive information as well. They interact through behaviour or functions, which are better known as Message

Passing. This concept is applicable to real world objects as well as software objects.

Functions

Software Object (Student)

Function

Data

Object B

Elementary Concept^ of^ Objects^ and^ Classes^

31

Class

As discussed above, an object is a unique entity possessing some characteristics

and behaviour. You might be thinking that from where these characteristics and

behavior are embedded in an object'. It is done with the help of a 'Class'. Basically

a 'Class' is a blue print or template for its objects. It can also be defined as a

category that describes some characteristics and behaviour. Each object of a clas

possesses the characteristics and the behaviour (data and functions) described

within the class. In this way, a Class' and its Objects' have a close relationship

in an Object Oriented Programming System. They can be considered to be the

two sides of the same coin, which cannot be disintegrated. It is insignificant to

discuss an object without referring to its 'Class'.

If we define a class 'Fruits', then it may describe the following characteristics

and behaviour:

Characteristics colour

32

taste

shape

Apple (object 1)

used for making juice

Creating objects of a class

Common behaviour

used for festive occassions

Different types of fruits viz. apple, banana, pear, grapes are referred to as

objects under that category. It means that the different types of fruits are the

objects which belong to the class "Fruits'.

used for breakfast

(object 2)

Fruits (class)

(Different types of fruits) Banana Pear (object 3)

Here, each object of the class ´Fruits' will have different characteristics with

common behaviour (shown in the table above).

Grapes

Understanding (^) Computer (^) Applications with (^) Blue<--IX

(object 4)

An object is also called an instance of the class.

to be Creating^ an^ object^ of^ a^ class^ is^ said

instantiation. All the (^) instances share the (^) state (^) (attribute) and (^) the

described within the class. behaviour

But, the attributes (i.e. the states) are unique for

object. A^ single class may each

have any number of instances.

As the objects of class Car have been created, they will follow the structure as shown below:

34

Different Characteristics

Common Behaviour

class Flower

Characteristics: Colour Smell Behaviours:

Honda

Decoration

Colour

If you want to go for a journey on a Honda then the characteristics of Honda will be used. Similarly, it can also be understood for the

objects Nano and Maruti.

Worship

Model

Here, each object of the class 'Car' will possess different characteristics like

colour and (^) mnodel but (^) will have (^) common behaviour like Journey and Demo.

statements:

Nano

" Let (^) us take (^) another (^) example. (^) Here, we (^) are (^) going to (^) assume (^) Flower' as (^) a class. The blue print or template of the class Flower' can be designed as shown below:

Rose

Colour

(object 1)

Model

Journey Demo

Understanding Computer Applications with Bluel-IX

Flower class

Aclass is simply a representation of similar types i of Objects. It is the blueprint/ i plan/ template that describes I the details of an Object.

Lily (object 2)

Maruti

Flower rose = new Flower();

Colour

Flower lily =^ new (^) Flower();

Model

Flower lotus = (^) new (^) Flower();

Now, the objects of the class Flower' can be

created by (^) using the (^) following

Lotus (object 3)

Here, the objects rose, lily and lotus are referred to as class tags or

of the instances

class. It means each object of the class 'Flower' will follow the

blue print

and willpossess the characteristics and behaviour defined within it. Have a look

on the table shown below:

Colour

Rose Red Smell Yes

Different Characteristics

Colour

Lily

Just to Know

Yellow Colour Smell Yes Common Behaviour Decoration Garland Worship

Software Class and Objects

The table shown above illustrates that the objects rose, lily and lotus possess

different characteristics but they have common behaviour. The behaviour mainly refers to the functions to be carried out, on different objects. To carry the functions on different objects, the statements can be written as:

Lotus

rose. Decoration:It means the characteristics (Colour, Smell) of rose will be used for decoration.

Pink

lily. Decoration : It means the characteristics (Colour, Smell) of lily will be

used for decoration.

Smell No

lotus. Worship : It means the characteristics (Colour, Smell) of lotus will be

used for worship.

Atypical Java class creates many objects and interacts by invoking methods. An object consists of:

  1. State: It is represented by the attributes of an object. It also reflects the properties of an object.
  2. Behaviour: It is represented by the methods of an object. It also reflects the relation of an object with the other objects.

Now, we will^ take^ examples^ of^ software^ class and objects that are used for programming

purpose in Java language.

  1. ldentity: It^ gives^ a^ unique name^ to^ an^ object^ and^ enables^ an^ object^ to^ interact with the other objects.

Let, 'Employee^ be^ a^ class^ that^ describes

the following^ data^ and^ member^ methods:

class Employee

//Data Members Name

Dept

Salary

//Member Methods AceptData( )

DisplayData( )

Elementary Concept of Objects and Classes 35

Once, the objects are created it will possess data members and member

methods as shown below:

Quantity

Rate Amount

and so on...

Note

TV set

class Apple

Data members

4

public void Season()

50, 2,00,

Quantity

public void Season()

Rate Amount Member Methods

InputVal( )

Calculate( ) Display( ) The member methods will be invoked on each object as: tvset.InputVal( ) :It will accept the Quantity and Rate of the tvset.

cooler.InputVal( ) : It will accept the Quantity and Rate of the cooler.

tvset.Calculate( ) :It will calculate the amount for the tvset. tvset.Display( ) It will display the amount for the tvset.

Cooler 5 8,

Since, youare not aware of the data types and writing Java statements, in the above examples of software class and objects, neither data types are mentioned nor the methods are defined. The templates and explanations are only for understanding the inter-relationship of class with its objects.

40,

By now, you would have learnt about the class, objects and their inter

relationship. Now, we are going to write an executable program by using different classes and their objects. As you are not aware of data types, variables

and constants hence, the classes are available without the data members but are

defined only with member functions.

1/a sample program to create different objects through methods

class Mango

System.out.printn("Mango is the king of fruits");

System.out.println("It is available during Summer season");

Elementary Concept of Objects and Classes 37

System.out.println("Applesare producedin Kashmir"); System.out.printin("Theyare availableduring Winter season");

classGrape

publicvoidSeason()

System.out.println("Grapesare producedin Maharashtra"): System.out.println("Theyare availableduring Winter season");

publicclassSample_Fruits

publicstaticvoid main(Stringl]args)

MangoFruitl= new Mango();

AppleFruit2= new Apple();

Grape Fruit3= new Grape();

Fruit1.Season();

Fruit2.Season();

Fruit3.Season();

Compilationof the program:

SampleFuits ClassEdit Tools Options CompileUndoCut Copy Paste SourceCode 1/à sample programto create differentobjectsthroughmethods classMango { publicvoid Season() System. out.println("Mangois the kingof fruits"): System. out.println("Itis availableduringSummer season"):

classApple { publicvoid Season() System.out.println("Theyare availableduringWinter season");

System. out.println("Applesare producedin Kashmir")

classGrape publicvoid Season()

saved

System.out .printIn("Grapesare producedin Maharashtra"); Class compiled-nosyntaxerrors

(^38) Unders Compu Applicwith Blue<

Find..Close

allowsusto model real-worldobjects.User definedclasses combine

the data

and methodsas integrated components.

a user may createa data typeand

declare

Thus,we cansay thatJava languageuses some pre-defined

data typesviz,.

int, float,char,etc. Thesedata types providesome preliminary

facilitiesin Java

programming.Whentheyare usedto declareany variable,

thenthe

would possessits built-in characteristics.Hence,these types

are calledbuilt

characteristicsand behaviourwithinit. Thiscanbe doneby usinga class.

This

is the reasonwhya classis referredtoas user defineddata ype.

in data types. Similarly,

Objectis an instanceof a class

In Java language,each objectis createdfroma classandsois calledan

instanceof

that class. Creatingan instanceofa classis sometimesreferredtoas

theclass.

A real-worldexampleof an object wouldbea Doberman',whichis an

instanceofa class called'Dog'.We have already discussedthatthe data members

of a classare also referredtoas instance variables.The instance variablesget

embedded automaticallywithinan objectat the timeof its

States (attributes)and Methods

Someof the attributesof the objectsof a classare explainedbelow:

ClassTelevision ClassStudent

Methods

(Functions)

State (Characteristics)

Methods

(Functions)

State (Characteristics)

Company Name

Model Getdetails() Adm_No. Accept()

Size Attributes Display() Date_ofbirth Attributes Display()

Price Aadhar_No.

DifferencesbetweenClassand Object

Class Object

some characteristicsand behaviour.

  1. It isa representationof an abstraction1. It isa realand uniqueentity having

only.

operator.

2.It is an object producerand 2. It is createdwiththe helpof the 'new

  1. Itis knownas the 'Object Factory'.3. Itis knownas an Instanceofa Class'.

(^40) Understa Compute Applicatwith Blue<-IX

creation.Hence,an objectis referredtoas an instanCeofa class.

ofdifferent Objectsof a Class

The characteristics(data members) describedwithina classare alsoknownas

States orAttributesforthe objectsofthat class.

Declaring

hence calleda blue printfora setof objects.