Object-Oriented Programming (CS F213) : BITS Pilani

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Object-Oriented Programming (CS F213)

Module I: Object-Oriented and Java Basics


CS F213 RL1.1: Object and Class Basics

BITS Pilani Dr. Pankaj Vyas


Department of Computer Science, BITS-Pilani, Pilani Campus
CS F213 RL 1.1 : Topics

• What is an Object ?
• Graphical View of an Object
• Object Examples
• What is a Class ?
• Object vs Class ?
• Class Examples

2 Object-Oriented Programming (CS F213)


What is Object ?

• Object Means Combination of Data (Attributes)


and Logic (Algorithm, Behavior, Functions,
Operations) of some real world entity. For
example Student, Box, Account, Time
• Every real-world object has two characteristics :
o Data-Part/State [Also known as attributes or
properties]
o Behavior [Also known as operations / Algorithmic /
Logic Part]
• Software Object is conceptually similar to a every
real-world object.
3 Object-Oriented Programming (CS F213)
Object: Graphical View

Object Keeps State/Data Part and


Behavior/Logic Part Together

State/Data
Part

Operations/Logic/Algorithmic Part

A Software
Object

4 Object-Oriented Programming (CS F213)


Object Examples

• Box Object :
o State/Data Part : length, width, height, Color [Attributes/Instance Fields]
o Behavior Part: computing area, computing volume [Operations, Methods]

• Dog Object :
o State/Data Part : name, breed, color [Attributes / Instance Fields]
o Behavior Part : barking, fetching, wagging [Operations, Methods]

• Account Object :
o State/Data Part : account number, account holder name, balance, type
of account [Attributes/ Instance Fields]
o withdrawing an amount, depositing an amount, checking balance of a
account [ Behavior, Operations, Methods]

5 Object-Oriented Programming (CS F213)


Object Examples
Box as a Real World Box as a Software Object
Object
computing area
length,
width,
height,
color
computing volume

Dog as a Software Object


Dog as a Real World
Object
barking
name,
wagging breed,
color
fetching

6 Object-Oriented Programming (CS F213)


What is Class ?

• Objects are grouped in classes


• A class is a collections of objects having similar
behavior and attributes
• An object is simply a single instance of class.
• Objects can not be instantiated (or created)
without defining a class
• Classes are defined whereas objects are
created.
• In order to create an object, you have to first
define the class of that object
7 Object-Oriented Programming (CS F213)
Class Example : Box Class
class Box
{
private double length;
private double width; Instance Fields
private double height;
public double area()
{
return 2* (length * width + width * height + height * length);
}
public double volume() Methods
{
return length * width * height;
}
}// End of class

8 Object-Oriented Programming (CS F213)


Class Example : Box Class
class Box
{
private double length;
Box Class Name
private double width;
- length: double
private double height;
public double area()
- width: double Attributes
- Height: double
{
return 2* (length * width + width * height + height * length); +area() : double
} +volume(): double Methods
public double volume()
{
return length * width * height;
}

:Box
}// End of class

Box b1 = new Box(); b1 length width height

:Box
Box b2 = new Box(); b2 length width height

9 Object-Oriented Programming (CS F213)


Class Example : Point
class Point
{ Point
private double x;
private double y; - x: double
- y: double
public double getX()
+getX() : double
{ +getY(): double
return x; +setX(value:double):void
} +setY(value:double):void
public double getY()
{ Point p1 = new Point();
return y;
} :Point
public void setX(double value)
{
x = value; p1 x y
}
public void setY(double value)
{ Point p2 = new Point();
:Point
y = value;
}
}// End of class
p2 x y

10 Object-Oriented Programming (CS F213)


Exercise

• Think about the class named ‘Student’. Define its state


and operations
• Define the instance fields (attributes) and methods of
class named ‘Line’.

11 Object-Oriented Programming (CS F213)

You might also like