Sunteți pe pagina 1din 20

Object-oriented Programming An Introduction

Snapshots Object- oriented programming An Introduction

What is an Object?
Encapsulation The Benefits of Encapsulation

What Are Messages?


The Benefits of Messages What are Classes?

The Term "Object"


The Benefits of Classes

Rel. Feb. 2000

1.1

STG Intl. Ltd.

Object-oriented Programming An Introduction

What is Inheritance? Abstraction Polymorphism Dynamic Binding

Overloading
Overriding Benefits of OOPS

Rel. Feb. 2000

1.2

STG Intl. Ltd.

Object-oriented Programming An Introduction

Objectives
After studying this chapter, the students should be able to: Understand the concepts of Object Oriented Methodology Abstraction, Encapsulation, Objects, Classes and Inheritance.

Object- Oriented Programming An Introduction Object-Oriented Programming (OOP) has been developed, based on real life itself, to easily and efficiently remove the problems associated with procedural programming. The first achievement of OOP towards better design and maintenance is the removal of ambiguity, OOP handles is the problem of non-flexibility of code.
Rel. Feb. 2000 1.3 STG Intl. Ltd.

Object-oriented Programming An Introduction

What is an Object? Objects are software bundles of data and related procedures. The following illustration is a common visual representation of a software object:

An Object
Rel. Feb. 2000 1.4 STG Intl. Ltd.

Object-oriented Programming An Introduction

Encapsulation

Your Bicycle

Rel. Feb. 2000

1.5

STG Intl. Ltd.

Object-oriented Programming An Introduction

The Benefits of Encapsulation Encapsulating related variables and methods into a neat software bundle is a simple yet powerful idea that provides two primary benefits to software developers: Modularity Information hiding What Are Messages? Software objects interact and communicate with each other by sending messages to each other. When object A wants object B to perform one of its methods, object A sends a message to object B.

Rel. Feb. 2000

1.6

STG Intl. Ltd.

Object-oriented Programming An Introduction

Messaging
Rel. Feb. 2000 1.7 STG Intl. Ltd.

Object-oriented Programming An Introduction

A Message with Parameters

Rel. Feb. 2000

1.8

STG Intl. Ltd.

Object-oriented Programming An Introduction

Three components comprise a message:


the object to whom the message is addressed (bicycle) the name of the method to be performed (changing gears) any parameters needed by the method (to a higher gear) The Benefits of Messages Everything an object can do is expressed through its methods, so message passing supports all possible interactions between objects. Objects don't need to be in the same process or even on the same machine to send and receive messages back and forth to each other.

What are Classes? A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.
Rel. Feb. 2000 1.9 STG Intl. Ltd.

Object-oriented Programming An Introduction

A Class

Rel. Feb. 2000

1.10

STG Intl. Ltd.

Object-oriented Programming An Introduction

The Bicycle Class

Rel. Feb. 2000

1.11

STG Intl. Ltd.

Object-oriented Programming An Introduction

The Term "Object"

In the real world its obvious that classes are not themselves the objects that they describe.
Then also occurs because many people use the term object inconsistently and use it to refer to both classes and instances.

The main difference between a class and an object is that objects are tangible, but a class is always intangible. You cant see a class but you can always see an object.
The Benefits of Classes Objects provide the benefit of modularity and information hiding. Classes provide the benefit of reusability

Rel. Feb. 2000

1.12

STG Intl. Ltd.

Object-oriented Programming An Introduction

What is Inheritance?
Classes inherit state and behavior from their superclass. Inheritance provides a very helpful concept of code reusability.

Hierarchy of Classes
Rel. Feb. 2000 1.13 STG Intl. Ltd.

Object-oriented Programming An Introduction

The Benefits of Inheritance Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times. Programmers can implement superclasses that define "generic" behaviors (called abstract classes). The essence of the superclass is defined and may be partially implemented but much of the class is left undefined and unimplemented. Other programmers fill in the details with specialized subclasses. Abstraction In java, classes are used to categorize data in order to model real life systems. Abstraction is this process of categorizing data.
Rel. Feb. 2000 1.14 STG Intl. Ltd.

Object-oriented Programming An Introduction

Abstraction
Rel. Feb. 2000 1.15 STG Intl. Ltd.

Object-oriented Programming An Introduction

Polymorphism Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate, actions.
Shape Draw ( )

Circle Object Draw (circle )

Box Object Draw (box )

Triangle Object Draw (triangle )

Polymorphism
Rel. Feb. 2000 1.16 STG Intl. Ltd.

Object-oriented Programming An Introduction

Dynamic Binding Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.

Overloading
In Object Oriented Programming if a method depending on some information does different job at different times that method is overloaded .Overloading is nothing but a kind of polymorphism.
Rel. Feb. 2000 1.17 STG Intl. Ltd.

Object-oriented Programming An Introduction

Example:

Suppose drawGraph() is a mechanism to draw a graph depending on the information provided,


DrawGraph(): Draws a point at any random location. DrawGraph(point1, point2) Draws a line from point1 to point2 DrawGraph(point1,point2,p Draw a rectangle using these points. oint3,point4)

Rel. Feb. 2000

1.18

STG Intl. Ltd.

Object-oriented Programming An Introduction

Overriding Inheritance is a mechanism of inheriting the traits of parents but some times some properties should be modified according to the need like a son inherits legs from his parents but his walking style is different. This is Overriding. It is again a kind of polymorphism and very important in terms of dynamic decisions.

Benefits of OOP
Through inheritance, we can eliminate redundant code and extend the use of existing classes.

We can build programs from the standard working modules, which communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity.
1.19 STG Intl. Ltd.

Rel. Feb. 2000

Object-oriented Programming An Introduction

The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. It is possible to have multiple instances of an object to coexist without any interference. It is possible to map objects in the problem domain to corresponding objects in the program. It is easy to partition the work in a project based on objects. The data-centered design approach enables us to capture more details of a model in implementable form. Object-oriented systems can be easily upgraded from small to large systems. Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler. Software complexity can be easily managed.
1.20 STG Intl. Ltd.

Rel. Feb. 2000

S-ar putea să vă placă și