Object Basics Ppt

download Object Basics Ppt

of 50

Transcript of Object Basics Ppt

  • 7/28/2019 Object Basics Ppt

    1/50

    Object BasicsObject BasicsChapter 2

  • 7/28/2019 Object Basics Ppt

    2/50

    IntroductionIntroduction If there is a motivation behind object-

    oriented system development, it is thedesire to make the software development

    easier and more natural by rising the

    level of abstraction to the point whereapplications can be implemented in the

    same terms in which they are described

    by users.

  • 7/28/2019 Object Basics Ppt

    3/50

    IntroductionIntroduction Let us develop an notion of an object

    through an example. A car is an object ,a real world entity,

    identifiably separate to with the

    surroun ngs. A car has a well-defined set ob attributes

    in relation to other objects such as

    color, manufacturer, cost, owner a welldefined set of thing you would usually dowith it drive, lock, carry passengers, etc.

  • 7/28/2019 Object Basics Ppt

    4/50

    IntroductionIntroduction In object orientation we call the

    attributes ad properties and actions asmethods.

    Pro erties describe the state of an

    object.

    Methods define the behavior.

  • 7/28/2019 Object Basics Ppt

    5/50

    An ObjectAn Object-- Oriented PhilosophyOriented Philosophy Most of the programming languages

    provide programmers with the way ofdescribing processes.

    Althou h most ro rammin lan ua es

    are computationally equivalent, the ease

    of description, reusability, extensibility,

    readability can vary depending upon thelanguage used.

  • 7/28/2019 Object Basics Ppt

    6/50

    An ObjectAn Object-- Oriented PhilosophyOriented Philosophy A natural language or programming,

    provides its users a base set ofconstructs.

    Man ro rammin derive their basic

    ideas from the underlying machine.

    The machine may understand types such

    as integers, floating point numbers andthe programming language will represent

    them as structures.

  • 7/28/2019 Object Basics Ppt

    7/50

    An ObjectAn Object-- Oriented PhilosophyOriented Philosophy A fundamental characteristics of object

    oriented programming is that it allows thebase concepts of the language to extendto include ideas and terms closer to

    . New data types can be defined in terms

    of existing data types.

    Using data abstraction mechanism , it ispossible to create new higher-level andmore specialized types.

  • 7/28/2019 Object Basics Ppt

    8/50

    An ObjectAn Object-- Oriented PhilosophyOriented Philosophy The fundamental difference between the

    object-oriented system and their traditionalcounterparts is the way in which youapproach problems.

    are either algorithm-centric or data centric.

    In algorithm-centric methodology, you thinkof an algorithm that can accomplish the task,then build data structures for that algorithmto use.

  • 7/28/2019 Object Basics Ppt

    9/50

    An ObjectAn Object-- Oriented PhilosophyOriented Philosophy In data-centric methodology, you think

    how to structure the data, then build thealgorithm around that structure.

    In Ob ect-oriented s stem however the

    algorithm and the data structure arepackaged together as an object, which has

    a set of attributes and properties.

    The state of these attributes are reflected

    in values stored in its data structures.

  • 7/28/2019 Object Basics Ppt

    10/50

    An ObjectAn Object-- Oriented PhilosophyOriented Philosophy The object has collection of methods or

    procedures things it can do. The methods and attributes are equal

    and inse arable arts of the ob ect one

    cannot ignore one for the sake of other.

    Traditional programming tends to write

    lots of code to do all the things that haveto be done.

  • 7/28/2019 Object Basics Ppt

    11/50

    An ObjectAn Object-- Oriented PhilosophyOriented Philosophy In summary , object oriented

    programming language bridge thesemantic gap between the ideas of the

    application and those of the underlying

    machine, and objects represent theapplication data in a way that is not forced

    by hardware architecture.

  • 7/28/2019 Object Basics Ppt

    12/50

    ObjectsObjects The term Object was first formally

    utilized in Simula language. The term object means a combination of

    data and lo ic that re resents some real

    entity.

    In object-oriented system, everything is an

    object. Each of us deal with object daily.

  • 7/28/2019 Object Basics Ppt

    13/50

    ObjectsObjects When developing object-oriented

    applications, two basic questions alwaysarise:

    What ob ects does the a lication need?

    What functionality should those objects

    have?

  • 7/28/2019 Object Basics Ppt

    14/50

    Objects are grouped into classesObjects are grouped into classes It is fairly natural to partition the world

    into objects, properties and procedures. This is useful for classification

    have no trouble in identifying it, as eagle

    and not airplane, even if we have not seen

    that bird before. Classes are used to distinguish one type

    of object from another.

  • 7/28/2019 Object Basics Ppt

    15/50

    Objects are grouped into classesObjects are grouped into classes In the context of object-oriented systems,

    a class is a set of objects that share acommon structure and a common

    behavior.

    A single object is simply instance of aclass.

    The chief role of a class is to define theproperties and procedures.

  • 7/28/2019 Object Basics Ppt

    16/50

    Attributes : Object State andAttributes : Object State and

    PropertiesProperties Properties represent the state of an

    object. It is basically used to refer the description

    of an ob ect.

    For Examples:

    Car has properties such as color,

    manufacturer, and cost. Properties could be represented in

    several ways in programming langugage

  • 7/28/2019 Object Basics Ppt

    17/50

    Attributes : Object State andAttributes : Object State and

    PropertiesProperties For Color , we could choose either String

    RED or an integer representing RED. Manufacturer could be represented by a

    name which is a reference in

    Manufacturer object.

    Cost could be a floating point number.

    The importance of distinction is that anobjects abstract state can be

    independent of its physical state.

  • 7/28/2019 Object Basics Ppt

    18/50

    Object Behavior and MethodsObject Behavior and Methods When we talk about an elephant or a car

    we can describe the set of things we cando with it or that it can do in its own.

    This refers to descri tion of ob ects

    behavior.

    In OO object behavior is described in

    methods or procedures. A method implements a behavior of an

    object.

  • 7/28/2019 Object Basics Ppt

    19/50

    Object Behavior and MethodsObject Behavior and Methods The object, called the receiver, is that on

    which the method operates. Methods encapsulates the behavior of the

    object.

    Methods are used to exclusively accessand update the properties of the object.

    Objects can take responsibility of their

    own behavior. For ex: the Employee object know how to

    compute its own salary.

  • 7/28/2019 Object Basics Ppt

    20/50

    Objects Respond to MessagesObjects Respond to Messages An objects capabilities are determined by

    the methods defined for it. Methods conceptually are equivalent to

    the function definitions used in

    proce ura anguage. Objects perform operations in response

    to messages.

    For Ex: when you press the brake a pedalof the car, you send a stop message to thecar object.

  • 7/28/2019 Object Basics Ppt

    21/50

    Objects Respond to MessagesObjects Respond to Messages The car object knows how to respond to

    the stop message. Sending the stop message to other

    ob ects like tree will be meanin less and

    could lead to unanticipated response.

    Messages essentially are non specific

    function calls: We could send a drawmessage to a chart when we want to

    chart object to draw itself.

  • 7/28/2019 Object Basics Ppt

    22/50

    Objects Respond to MessagesObjects Respond to Messages A message is different from a subroutine

    call , since different objects can respondto the same message in different ways.

    For exam le: car motorc cle and bic cle

    respond to a stop message, but actualoperations performed are object specific.

    It is important to understand thedifference between methods and

    messages.

  • 7/28/2019 Object Basics Ppt

    23/50

    Objects Respond to MessagesObjects Respond to Messages Example:

    Say you .want to tell someone to makeyou a French Onion Soup, your

    instruction is the messa e and the wa

    the soup is prepared is the method.

    In other words , the message is a

    instruction and the method is theimplementation.

  • 7/28/2019 Object Basics Ppt

    24/50

    Encapsulation and InformationEncapsulation and Information

    HidingHiding Information hiding is the principle of

    concealing the internal data andprocedures of an object and providing aninterface to each object in such a way as

    inner workings.

    In C++ there are very general

    encapsulation protection mechanismwith public , private and protectedmembers.

  • 7/28/2019 Object Basics Ppt

    25/50

    Encapsulation and InformationEncapsulation and Information

    HidingHiding Public members (member data and

    member functions) may be accessed fromanywhere.

    Private members are accessible onl from

    within the class.

    Protected members are only accessed

    from sub classes.

  • 7/28/2019 Object Basics Ppt

    26/50

    Encapsulation and InformationEncapsulation and Information

    HidingHiding Often an object is said to encapsulate the

    data and a program.

    This means that the user cannot see theinside of the object capsule, but can use

    .

    Encapsulation or information hiding is adesign goal of an object-oriented system.

    Rather than allowing an object direct accessto another objects data , a message is sentto the target object requesting information.

  • 7/28/2019 Object Basics Ppt

    27/50

    Encapsulation and InformationEncapsulation and Information

    HidingHiding Another issue is per-object or per-class

    protection. In per-class protection, the most common

    form e. . ADA C++ Eiffel class

    methods can access any object of thatclass and not just the receiver.

    In per-object protection, methods canaccess only the receiver.

  • 7/28/2019 Object Basics Ppt

    28/50

    Encapsulation and InformationEncapsulation and Information

    HidingHiding An important factor in achieving

    encapsulation is the design of differentclasses of objects that operate using a

    common protocol, or objects user

    interface. This means a program can send a generic

    message and leave the implementation

    up to the receiving object.

  • 7/28/2019 Object Basics Ppt

    29/50

    Class HierarchyClass Hierarchy An object-oriented system organizes

    classes into a subclass-superclasshierarchy.

    At the to of the class hierarch are the

    most general classes and at the bottomare the most specific.

    The sub class inherits all the propertiesand methods defined in its superclass.

  • 7/28/2019 Object Basics Ppt

    30/50

    Class HierarchyClass Hierarchy

  • 7/28/2019 Object Basics Ppt

    31/50

    InheritanceInheritance Inheritance is a property of object-

    oriented systems that allows objects tobe built from other objects.

    It takes the advanta e from the

    commonality of objects whenconstructing new classes.

    Inheritance is the relationship betweenthe classes where one is parent class of

    another (derived) class.

  • 7/28/2019 Object Basics Ppt

    32/50

    InheritanceInheritance Parent class is also known as super class

    or base class. The main advantage of this technique is

    that we can build on what we alread

    have and more, important, reuse what wealready have.

    Inheritance allows us to share and reuse

    the behaviors and attributes.

  • 7/28/2019 Object Basics Ppt

    33/50

    InheritanceInheritance

    Ford

    Mustang Taurus Thunderbird

  • 7/28/2019 Object Basics Ppt

    34/50

    Dynamic InheritanceDynamic Inheritance Dynamic inheritance allows objects to

    change and evolve overtime.

    Since base classes provide properties andattributes for objects, changing the base

    c asses c anges t e propert es anattributes of a class.

    More specifically, dynamic inheritance

    refers to the ability to add, delete, orchange parents from objects(or classes )at runtime.

  • 7/28/2019 Object Basics Ppt

    35/50

    Multiple InheritanceMultiple Inheritance Some object-oriented systems permit a

    class to inherit its state(attributes) andbehaviors from more than one superclass.

    This kind of inheritance is referred to as

    multiple inheritance. Multiple inheritance can pose some

    difficulties

  • 7/28/2019 Object Basics Ppt

    36/50

    Multiple InheritanceMultiple Inheritance Example: Several distinct parents can

    declare a member within a multipleinheritance hierarchy.

    One wa of achievin benefits of multi le

    inheritance is to inherit appropriate classand then add an object of another class as

    an attribute.

  • 7/28/2019 Object Basics Ppt

    37/50

    PolymorphismPolymorphism Poly means many and morph means

    forms.

    It means objects that can take and assumemany forms.

    Pol mor hism means that the same

    operation may behave differently ondifferent classes.

    For example, consider how driving anautomobile with a manual transmission isdifferent from driving a car with anautomatic transmission.

  • 7/28/2019 Object Basics Ppt

    38/50

    PolymorphismPolymorphism It is the fundamental concept in OOS.

    Polymorphism allows us to write generic,reusable code more easily.

  • 7/28/2019 Object Basics Ppt

    39/50

    Object Relationships andObject Relationships and

    AssociationsAssociations Association represents the relationship

    between objects and classes. For examples: A pilot can flyplanes

    pilot and planes entities.

    Associations are bidirectional; that means

    they can be traversed in both directions,perhaps with different connotations.

  • 7/28/2019 Object Basics Ppt

    40/50

    Object Relationships andObject Relationships and

    AssociationsAssociations A direction implied by a name is the

    forward direction ; the opposite directionis the inverse direction.

    Can fl connects a ilot to certain

    airplanes. The inverse of can fly could be called

    flown by.

    Ob R l h dOb R l h d

  • 7/28/2019 Object Basics Ppt

    41/50

    Object Relationships andObject Relationships and

    AssociationsAssociations Another important issue in association is

    cardinality, which specifies how manyinstances of one class may relate to a

    single instance of an associated class.

    Cardinality constraints the number ofrelated objects and often is described as

    being one or many.

  • 7/28/2019 Object Basics Ppt

    42/50

    ConsumerConsumer--Producer AssociationProducer Association A special form of association is a

    consumer producer relationship, alsoknown as a client server association or a

    use relationship.

    The consumer-producer relationship canbe viewed as one way interaction: One

    object requests the service of another

    object.

  • 7/28/2019 Object Basics Ppt

    43/50

    ConsumerConsumer--Producer AssociationProducer Association The object that makes request is the

    client or consumer, and the object thatreceive request or provide service is the

    producer or server

    Print Server Item

    Request for service

    A i d bjA i d bj

  • 7/28/2019 Object Basics Ppt

    44/50

    Aggregation and objectAggregation and object

    ContainmentContainment All objects, except most basic ones, are

    composed of and may contain otherobjects.

    For exam le Car ob ect is com osed of

    engine and engine is still composed ofshafts and valves.

    Breaking down the objects into the object

    from which they are composed is called

    decomposition.

    A ti d bj tA ti d bj t

  • 7/28/2019 Object Basics Ppt

    45/50

    Aggregation and objectAggregation and object

    ContainmentContainment Since each object has identity, one object

    can refer to other objects. This is known as aggregation, where

    attribute can be ob ect itself.

  • 7/28/2019 Object Basics Ppt

    46/50

    Object and IdentityObject and Identity A special feature of object-oriented

    systems is that every object has its ownunique and immutable identity.

    An ob ect identit comes into bein

    when the object is created and continuesto represent that object then on.

    The identity is never confused with

    another objects identity even if the object

    is deleted.

  • 7/28/2019 Object Basics Ppt

    47/50

    Object and IdentityObject and Identity The identity name never changes even if

    the properties of the object change- itsindependent of the objects state.

    In ob ect oriented s stems ob ect

    identity is implemented through somekind of object identifier(OID) or unique

    identifier(UID).

    An OID guarantees uniqueness of every

    identifier.

  • 7/28/2019 Object Basics Ppt

    48/50

    Static and Dynamic bindingStatic and Dynamic binding The process of determining (dynamically)

    at runtime which function to invoke istermed dynamic binding.

    Makin this determination earlier at

    compile time, is called static binding. Static binding optimizes the calls.

    Dynamic binding occurs whenpolymorphism calls are issued.

  • 7/28/2019 Object Basics Ppt

    49/50

    Object persistenceObject persistence Object have lifetime.

    They are explicitly created and can existfor a period of time that , traditionally, has

    been the duration of the rocess in

    which they were created. From language perspective this

    characteristics is called persistence.

  • 7/28/2019 Object Basics Ppt

    50/50

    Meta ClassesMeta Classes Information about classes is called meta

    class.