OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five...

35
OOP (Object Oriented Programming) Lecture 1

Transcript of OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five...

Page 1: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

OOP(Object Oriented Programming)

Lecture 1

Page 2: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Why a new paradigm is needed?

• Complexity

• Five attributes of complex systems– Frequently, complexity takes the form of a hierarchy– What is primitive for one observer may be at a much higher level

of abstraction for another– Components of a complex system are nearly decomposable,:

are not fully independent but understandable– Complex systems have common patterns. These patterns may

involve the reuse of small components– Complex systems will evolve from simple systems much more

rapidly

Page 3: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Why a new paradigm is needed?

• Due to complex nature of software it was hard to– Understand the software– Maintain the software

• So software engineers need a paradigm to cater this complexity

• Object Oriented Paradigm was solution to this problem

Page 4: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Which paradigms already exist?Paradigms Building Blocks

Procedure-oriented Algorithms

Object-oriented Classes and objects

Logic-oriented Goals, often expressed in a predicate calculus

Rule-oriented If-then rules

Constraint-oriented Invariant relationships

Page 5: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

What is OO paradigm

“Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships”

Page 6: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

What is OO paradigm

• There are three important parts to this definition:

• Object-oriented programming1. Uses objects, not algorithms, as its

fundamental logical building blocks

2. Each object is an instance of some class

3. Classes are related to one another via inheritance relationships

Page 7: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

The Object Model

• For all things object-oriented, the conceptual framework is the object model. There are four major elements of this model:– Abstraction– Encapsulation– Modularity– Hierarchy

A model without any one of these elements is not object-oriented

Page 8: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Abstraction

• Abstraction is one of the fundamental ways that we as humans cope with complexity

An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual

boundaries, relative to the perspective of the viewer.

An abstraction focuses on the outside view of an object, and so serves to separate an object's essential behavior from its implementation.

Page 9: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Abstraction

Page 10: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Abstraction

• Abstraction is to ignore the inessential details, dealing instead with the generalized, idealized model of the object

Page 11: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Encapsulation

• Encapsulation hides the details of the implementation of an object

• Encapsulation is most often achieved through information hiding– the process of hiding all the secrets of an

object that do not contribute to its essential characteristics

Page 12: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.
Page 13: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Modularity

• Modularization consists of dividing a program into modules which can be compiled separately, but which have connections with other modules.

• Making units of a large program (thing) to understand the units separately

Page 14: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Modularity packages abstractions into discrete units.

Page 15: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Hierarchy

• Hierarchy is a ranking or ordering of abstractions.

Page 16: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Level of Abstraction

Higher Abstraction Level

Lower Abstraction Level

Page 17: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Object & Class ….

These objects have different colors, locations & sizes.

Do they belong to the same class?

Page 18: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Characteristics of Objects

• An object has identity (it acts as a single whole).

• An object has state (it has various properties, which might change).

• An object has behavior (it can do things and can have things done to it).

Page 19: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Characteristics of Objects……

Consider a tube of four yellow tennis balls.

• Is the tube of tennis balls an object?

• Is each tennis ball an object?

• Could the top two balls be considered a single object?

• Is the color of the balls an object?

Page 20: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Characteristics of Objects……

• Is the tube of tennis balls an object?

• Yes. It has identity (my tube of balls is different than yours), it has state (opened, unopened, brand name, location), and behavior (although not much.)

• Is each tennis ball an object?

• Yes. It is OK for objects to be part of other objects. Although each ball has nearly the same state and behavior as the others, each has its own identity.

• Could the top two balls be considered a single object?

• Not ordinarily. Each has its own identity independent of the other. If they were joined together with a stick you might consider them as one object.

• Is the color of the balls an object?

• No. It is a property of each ball.

Page 21: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Objects

Page 22: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Software Objects• Question: What are software objects made out of?

• Answer: Computer memory.

• Software objects have identity because each is a separate chunk of memory

• Software objects have state. Some of the memory that makes a software object is used for variables which contain values.

• Software objects have behavior. Some of the memory that makes a software object is used to contain programs (called methods) that enable the object to "do things." The object does something when one of its method runs.

Page 23: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Picture of an Object• Variables: location, color,

and size.

• Methods: move and resize

• The methods must execute in the correct order.

• For an application the first

method to run is the

method named main().

• Have you seen a main() method before?

Page 24: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Class & Objects …

Class : MANAttributes:

Hair, eyes, face, Neck,

torso, hands, legs.

Fingers, height, weight,

age.

Methods:

walk(), talk(), sleep(),

Laugh(), cry(), dance(),

steady()

[Definition of Class Man]

Page 25: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Class

• A class is a representation for a conceptual grouping of similar items

• A class is a collection of objects

Page 26: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Class• A class is a description of a kind of object.

– A programmer may define a class

– or may use predefined classes that come in class libraries.

• A class is merely a plan for a possible object (or objects.). It does not by itself create any objects.

• When a programmer wants to create an object the new operator is used with the name of the class.

• Creating an object is called instantiation.

Page 27: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Using a Class to Make Many Objects

• Class is like cookies cutters and cookies are objects. There is only one cookie cutter, but can be used to make many cookies.

• Cookies can be created and cookies can be destroyed

• Different cookies may have different characteristics, even though they follow the same basic pattern

• Each cookie is made out of a different section of dough.

Page 28: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Real world objects and programming objects• Let’s consider an actual example: a

hospital• Some doctors provide services to patients• Patients finally buy medicines at the pharmacy.

• An object oriented application to manage a hospital would likely define the following objects:

• doctor,• patient,• medicines,• pharmacy.

Page 29: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Objects identity and equality: example - I

We introduce two families:1.The Johnsons,2.The Smiths.

Page 30: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Objects identity and equality: example - II

Both families have a lawn mower (to cut grass). Same color,same size, same brand. . .

Page 31: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Objects identity and equality: example - II

1. Both families have a lawn mower (to cut grass). Same color, same size, same brand. . .

2. . . . but two unique lawn mowers.

Page 32: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Objects identity and equality: example - III

1. Both families have a cat also. . .2. Johsons call it Tom, 3 years old, round, friendly, black and white colored.3. Smiths call it Felix, 3 years old, round, friendly, black and white colored.

Page 33: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Objects identity and equality: example - III

But actually they are feeding the same cat!

Page 34: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Objects identity and equality: example - IV

Page 35: OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.

Task 1

• You are expected to master the Java basics within first 15 days of the course

• This include– Data Variables– Loops and Logic– Arrays and Strings

• You can take help from the reference book provided with the lecture