Lecture#5 Classes
Lecture#5 Classes
Car
• Attributes:
• Gas in tank
Bank Account • Mileage
• Wheels
• Attributes:
• Behavior
• balance
• Accelerate
• Behavior
• Apply Brakes
• Deposit
• Load fuel
• Withdraw
• Check fuel
• Check balance
Objects Interact with each other
Get Loan
Bank Customer
Bank Account
What is object? An object is a collection of
data/atttributes and functions/behaviors.
The main aim of OOP is to bind together the data and the functions (that
operate on them) so that no other part of the code can access this data
except that function.
double width;
double length; Encapsulation refers
Data to the combining of
data and code into a
single object
displayWidth(){}
displayLength(){}
displayArea(){}
Functions Rectangle
Object
Classes and Objects
• A class is like a blueprint and objects are like houses
built from the blueprint.
• An object is an instance of a class
• A single class can have multiple instances
Class & Object in Object Oriented
Programming
Object:
•In OOP an object is a collection of data and functions.
•In OOP, programs are written on the basis of objects.
We visualize our programming problems in the form of objects and their
interactions.
To create different objects, classes are used.
Class:
- Classes are pieces of code blueprints which are used to
create objects.
- Class: It is the Collection of data (atttributes) and functions
(behaviors) related to that object .
• Format:
public class ClassName
{
variable declaration;
methods declaration;
}
public displayWidth(){}
public displayLength(){} Functions
public displayArea(){}
} width = 2
length = 5
width = 4
width = 4 length = 3
length = 2
Syntax of defining an Instance/object of
a Class
ClassName objectName =new constructor ;
struct Rectangle
Members of a class
{ are
double width; private by default
double length;
} r1;
class Rectangle
{
private double width;
private double length;
};
class main {
◾ You can see that Ali stores his personal information in itself
and its behavior is also implemented in it.
width
b1 height
depth
Box
object
b2
4
0
EXAMPLES