Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OOP-Lec3 (Introduction To OOP)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

1

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

Object Oriented Model


• Model:
A model is an abstraction of something real and conceptual e.g. Country
MAP.
• Object Orientation:
It is a technique in which we visualize our programming problems in the
form of objects and their interaction as happen in real life

For example:
we have different objects in our
surrounding which interact with each
other to perform different operations.
3

Example1: Objects’ interaction


• The objects in an educational institute
are
• Students
• Teachers
• Books
• Classroom
• Pen
• Department and so on…

• All these objects interact with each


other directly or indirectly to perform
certain actions

• By identify objects and their interaction we can solve the real world problem
easily in terms of real world objects.
4

Object Oriented Model


• In OOP, we concentrate over the object in contrast to procedural paradigm in
which we simply write our code in functions and call them in main function.

• 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

Example2: Objects’ interaction


6

Example3: Objects’ interaction


7

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

General Form of class


• A class specifies the data that it contains and the code that
operates on that data. While very simple classes may contain
only code or only data, most real-world classes contain both.

Instance variables to hold data

Operations to manipulate the data


10

General Form of class


• The data, or variables, defined within a class are called instance
variables.
• The code is contained within methods.
• Collectively, the methods and variables defined within a class
are called members of the class.
• Variables defined within a class are called instance variables
because each instance of the class (that is, each object of the
class) contains its own copy of these variables. Thus, the data
for one object is separate and unique from the data for another.
• Java classes do not need to have a main( ) method. You only
specify one if that class is the starting point for your program.
11

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

Class vs. Object


• A class defines a new type of data. In previous example, the
new data type is called Box.
• It is important to remember that a class declaration only creates
a template; it does not create an actual object
• Class name Box will be used to declare objects of type Box as:

• We can create multiple objects of class Box


• Every Box object will contain its own copies of the instance
variables width, height, and depth.
• To access these variables, the dot (.) operator is used as
13

Example1: Class and Objects

Output: Volume is 3000.0


14

Example2: Class and Objects

Output:
15

Object Declaration:
16

Assigning Object Reference Variables

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

Example: Method to the Box Class


20

Example: Method with returning value


21

Advantages of Object Orientation


• Modularity: It provides separation of duties in object-
based program development.
• Extensibility: Objects can be extended to include new
attributes and behaviors
• Reusability: Objects can also be reused within an across
applications
• Improved software maintainability
• Faster development
22

Thank You

You might also like