OOP-Lec3 (Introduction To OOP)
OOP-Lec3 (Introduction To OOP)
OOP-Lec3 (Introduction To OOP)
Contents
• Object Oriented Model
• What is an Object?
• Tangible and intangible Objects
• Class Fundamentals
• General Form of a Class
• A simple Class
• Object Declaration
• Object reference
• Method in Class
• Simple Method
• Method with return value
• Advantages of OOP
2
For example:
we have different objects in our
surrounding which interact with each
other to perform different operations.
3
• By identify objects and their interaction we can solve the real world problem
easily in terms of real world objects.
4
• For Example:
• In order to develop a system for Time table we will be focusing on the
objects involves in this process and we will find the interaction between
these object.
• Class is held in a specific room and teach by a particular teacher and
specific students study in this room at a particular time.
5
Object
• An object is something that has State (attributes), Well-defined
behavior (operations) and have unique identity
• It can be of two types
• Tangible (Ali, School, House, Car).
• Non-Tangible (time, date and so on).
• Tangible Objects:
• Objects which have physical existence
• e.g. Ali is a tangible object, having some characteristics (attributes) and
behavior. Ali can be identifying by name.
• Non-Tangible Objects:
• Objects which exist conceptually
• e.g. Song is a non-tangible object, having some characteristics (attributes)
and associated operations.
8
Class in OOP:
• Any concept you wish to implement in a Java program must be
encapsulated within a class.
• A class defines a new data type. Once defined, this new type
can be used to create objects of that type.
• Class is generic, it represent all members of a particular set
while object is specific.
• e.g. Class of Car will represent all cars while a specific car with number
ABC-123 is an object/instance of this class.
• The word Object and instance can be used interchangeably.
• In other words, a class is a template for set of objects, and an
object is an instance of a class.
9
A Simple Class
• As an example, here is a
class called Box that
defines three instance
variables:
• width
• height
• depth
• Currently, Box does not
contain any methods
12
Output:
15
Object Declaration:
16
When you assign one object reference variable to another object reference
variable, you are not creating a copy of the object, you are only making a
copy of the reference
17
Method in Class
• Classes usually consist of two things: instance variables and
methods.
• Type specifies the type of data returned by the method. This can
be any valid type, including class types that you create.
• Parameters are essentially variables that receive the value of the
arguments passed to the method when it is called. If the method
has no parameters, then the parameter list will be empty.
18
Method in Class
• Most of the time, you will use methods to access the instance
variables defined by the class.
• In fact, methods define the interface to most classes.
• In addition to defining methods that provide access to data, you
can also define methods that are used internally by the class
itself.
• For example:
• Since the volume of a box is dependent upon the size of the box, it
makes sense to have the Box class compute it. To do this, you
must add a method to Box
19
Thank You