1. OOP an Introduction

download 1. OOP an Introduction

of 20

Transcript of 1. OOP an Introduction

  • 7/31/2019 1. OOP an Introduction

    1/20

    STG Intl. Ltd.1.1Rel. Feb. 2000

    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

  • 7/31/2019 1. OOP an Introduction

    2/20

    STG Intl. Ltd.1.2Rel. Feb. 2000

    Object-oriented Programming An Introduction

    What is Inheritance? Abstraction

    Polymorphism

    Dynamic Binding

    Overloading

    Overriding

    Benefits of OOPS

  • 7/31/2019 1. OOP an Introduction

    3/20

    STG Intl. Ltd.1.3Rel. Feb. 2000

    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 andInheritance.

    Object- Oriented Programming

    An Introduction

    Object-Oriented Programming (OOP) has been developed,based on real life itself, to easily and efficiently remove theproblems associated with procedural programming.

    The first achievement of OOP towards better design andmaintenance is the removal of ambiguity,

    OOP handles is the problem of non-flexibility of code.

  • 7/31/2019 1. OOP an Introduction

    4/20

    STG Intl. Ltd.1.4Rel. Feb. 2000

    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 asoftware object:

    An Object

  • 7/31/2019 1. OOP an Introduction

    5/20

    STG Intl. Ltd.1.5Rel. Feb. 2000

    Object-oriented Programming An Introduction

    Encapsulation

    Your Bicycle

  • 7/31/2019 1. OOP an Introduction

    6/20

    STG Intl. Ltd.1.6Rel. Feb. 2000

    Object-oriented Programming An Introduction

    Encapsulating related variables and methods into a neatsoftware bundle is a simple yet powerful idea that provides twoprimary benefits to software developers:

    Modularity

    Information hiding

    What Are Messages?

    Software objects interact and communicate with each other by

    sending messagesto each other. When object A wants object Bto perform one of its methods, object A sends a message toobject B.

    The Benefits of Encapsulation

  • 7/31/2019 1. OOP an Introduction

    7/20 STG Intl. Ltd.1.7Rel. Feb. 2000

    Object-oriented Programming An Introduction

    Messaging

  • 7/31/2019 1. OOP an Introduction

    8/20 STG Intl. Ltd.1.8Rel. Feb. 2000

    Object-oriented Programming An Introduction

    A Message with Parameters

  • 7/31/2019 1. OOP an Introduction

    9/20

    STG Intl. Ltd.1.9Rel. Feb. 2000

    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 possibleinteractions between objects.

    Objects don't need to be in the same process or even on thesame 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 andthe methods common to all objects of a certain kind.

  • 7/31/2019 1. OOP an Introduction

    10/20

    STG Intl. Ltd.1.10Rel. Feb. 2000

    Object-oriented Programming An Introduction

    A Class

  • 7/31/2019 1. OOP an Introduction

    11/20

    STG Intl. Ltd.1.11Rel. Feb. 2000

    Object-oriented Programming An Introduction

    The Bicycle Class

  • 7/31/2019 1. OOP an Introduction

    12/20

    STG Intl. Ltd.1.12Rel. Feb. 2000

    Object-oriented Programming An Introduction

    In the real world its obvious that classes are not themselves theobjects 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 thatobjects are tangible, but a class is always intangible. You cantsee a class but you can always see an object.

    The Benefits of ClassesObjects provide the benefit of modularity and information hiding.Classes provide the benefit of reusability

    The Term "Object"

  • 7/31/2019 1. OOP an Introduction

    13/20

    STG Intl. Ltd.1.13Rel. Feb. 2000

    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

  • 7/31/2019 1. OOP an Introduction

    14/20

    STG Intl. Ltd.1.14Rel. Feb. 2000

    Object-oriented Programming An Introduction

    Subclasses provide specialized behaviors from the basis ofcommon elements provided by the superclass. Through theuse of inheritance, programmers can reuse the code in thesuperclass many times.

    Programmers can implement superclasses that define

    "generic" behaviors (called abstract classes). The essence ofthe superclass is defined and may be partially implementedbut much of the class is left undefined and unimplemented.Other programmers fill in the details with specializedsubclasses.

    Abstraction

    In java, classes are used to categorize data in order to model reallife systems. Abstraction is this process of categorizing data.

    The Benefits of Inheritance

  • 7/31/2019 1. OOP an Introduction

    15/20

    STG Intl. Ltd.1.15Rel. Feb. 2000

    Object-oriented Programming An Introduction

    Abstraction

  • 7/31/2019 1. OOP an Introduction

    16/20

    STG Intl. Ltd.1.16Rel. Feb. 2000

    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.

    ShapeDraw ( )

    BoxObject

    Draw (box )CircleObject

    Draw (circle )TriangleObject

    Draw (triangle )

    Polymorphism

  • 7/31/2019 1. OOP an Introduction

    17/20

    STG Intl. Ltd.1.17Rel. Feb. 2000

    Object-oriented Programming An Introduction

    Dynamic Binding

    Binding refers to the linking of a procedure call to the codeto be executed in response to the call.

    Dynamic binding means that the code associated with agiven 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 onsome information does different job at different times thatmethod is overloaded .Overloading is nothing but a kind ofpolymorphism.

  • 7/31/2019 1. OOP an Introduction

    18/20

    STG Intl. Ltd.1.18Rel. Feb. 2000

    Object-oriented Programming An Introduction

    Example:

    Suppose drawGraph() is a mechanism to draw a graph

    depending on the information provided,

    DrawGraph():

    DrawGraph(point1, point2)

    DrawGraph(point1,point2,p

    oint3,point4)

    Draws a point at any random

    location.Draws a line from point1 topoint2Draw a rectangle using thesepoints.

  • 7/31/2019 1. OOP an Introduction

    19/20

    STG Intl. Ltd.1.19Rel. Feb. 2000

    Object-oriented Programming An Introduction

    Inheritance is a mechanism of inheriting the traits of parents but

    some times some properties should be modified according tothe need like a son inherits legs from his parents but his walkingstyle 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 andextend the use of existing classes.

    Overriding

    We can build programs from the standard working modules,which communicate with one another, rather than having tostart writing the code from scratch. This leads to saving ofdevelopment time and higher productivity.

  • 7/31/2019 1. OOP an Introduction

    20/20

    STG Intl. Ltd.1.20Rel. Feb. 2000

    Object-oriented Programming An Introduction

    The principle of data hiding helps the programmer to buildsecure programs that cannot be invaded by code in otherparts of the program.

    It is possible to have multiple instances of an object to co-exist 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 externalsystems much simpler.

    Software complexity can be easily managed.