50
Object Basics Object Basics Chapter 2

Object Basics - Sushant's Technical Articles Basics Chapter 2. Introduction If there is a motivation behind object-oriented system development, it is the desire to make the software

  • Upload
    lymien

  • View
    217

  • Download
    5

Embed Size (px)

Citation preview

Object BasicsObject BasicsChapter 2

IntroductionIntroduction

� If there is a motivation behind object-oriented system development, it is the desire to make the software development easier and more natural by rising the easier and more natural by rising the level of abstraction to the point where applications can be implemented in the same terms in which they are described by users.

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

� A car has a well-defined set ob attributes in relation to other objects– such as color, manufacturer, cost, owner – a well defined set of thing you would usually do with it– drive, lock, carry passengers, etc.

IntroductionIntroduction

� In object orientation we call the attributes ad properties and actions as methods.

� Properties describe the state of an � Properties describe the state of an object.

� Methods define the behavior.

An ObjectAn Object-- Oriented PhilosophyOriented Philosophy

� Most of the programming languages provide programmers with the way of describing processes.

� Although most programming languages � Although most programming languages are computationally equivalent, the ease of description, reusability, extensibility, readability can vary depending upon the language used.

An ObjectAn Object-- Oriented PhilosophyOriented Philosophy

� A natural language or programming, provides its users a base set of constructs.

� Many programming derive their basic � Many programming derive their basic ideas from the underlying machine.

� The machine may understand types such as integers, floating point numbers and the programming language will represent them as structures.

An ObjectAn Object-- Oriented PhilosophyOriented Philosophy

� A fundamental characteristics of object oriented programming is that it allows the base concepts of the language to extend to include ideas and terms closer to those of its application.those of its application.

� New data types can be defined in terms of existing data types.

� Using data abstraction mechanism , it is possible to create new higher-level and more specialized types.

An ObjectAn Object-- Oriented PhilosophyOriented Philosophy

� The fundamental difference between the object-oriented system and their traditional counterparts is the way in which you approach problems.

� Most traditional development methodologies � Most traditional development methodologies are either algorithm-centric or data centric.

� In algorithm-centric methodology, you think of an algorithm that can accomplish the task, then build data structures for that algorithm to use.

An ObjectAn Object-- Oriented PhilosophyOriented Philosophy

� In data-centric methodology, you think how to structure the data, then build the algorithm around that structure.

� In Object-oriented system, however the � In Object-oriented system, however the algorithm and the data structure are packaged 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.

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 inseparable parts of the object; one and inseparable parts of the object; one cannot ignore one for the sake of other.

� Traditional programming tends to write lots of code to do all the things that have to be done.

An ObjectAn Object-- Oriented PhilosophyOriented Philosophy

� In summary , object oriented programming language bridge the semantic gap between the ideas of the application and those of the underlying application and those of the underlying machine, and objects represent the application data in a way that is not forced by hardware architecture.

ObjectsObjects

� The term Object was first formally utilized in Simula language.

� The term object means a combination of data and logic that represents some real data and logic that represents some real entity.

� In object-oriented system, everything is an object.

� Each of us deal with object daily.

ObjectsObjects

� When developing object-oriented applications, two basic questions always arise:

� What objects does the application need?� What objects does the application need?

� What functionality should those objects have?

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

� Example: When a eagle flies over us we � Example: When a eagle flies over us we 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.

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 a common structure and a common behavior.behavior.

� A single object is simply instance of a class.

� The chief role of a class is to define the properties and procedures.

Attributes : Object State and Attributes : Object State and PropertiesProperties� Properties represent the state of an

object.

� It is basically used to refer the description of an object.of an object.

� For Examples:

� Car has properties such as color, manufacturer, and cost.

� Properties could be represented in several ways in programming langugage

Attributes : Object State and Attributes : 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 name which is a reference in Manufacturer object.

� Cost could be a floating point number.

� The importance of distinction is that an object’s abstract state can be independent of its physical state.

Object Behavior and MethodsObject Behavior and Methods

� When we talk about an elephant or a car we can describe the set of things we can do with it or that it can do in its own.

� This refers to description of object’s � This refers to description of object’s behavior.

� In OO object behavior is described in methods or procedures.

� A method implements a behavior of an object.

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 access � Methods are used to exclusively access and 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.

Objects Respond to MessagesObjects Respond to Messages

� An object’s capabilities are determined by the methods defined for it.

� Methods conceptually are equivalent to the function definitions used in procedural language.procedural language.

� Objects perform operations in response to messages.

� For Ex: when you press the brake a pedal of the car, you send a stop message to the car object.

Objects Respond to MessagesObjects Respond to Messages

� The car object knows how to respond to the stop message.

� Sending the stop message to other objects like tree will be meaning less and objects like tree will be meaning less and could lead to unanticipated response.

� Messages essentially are non specific function calls: We could send a draw message to a chart when we want to chart object to draw itself.

Objects Respond to MessagesObjects Respond to Messages

� A message is different from a subroutine call , since different objects can respond to the same message in different ways.

� For example: car , motorcycle, and bicycle � For example: car , motorcycle, and bicycle respond to a stop message, but actual operations performed are object specific.

� It is important to understand the difference between methods and messages.

Objects Respond to MessagesObjects Respond to Messages

� Example:

� Say you .want to tell someone to make you a French Onion Soup, your instruction is the message and the way instruction is the message and the way the soup is prepared is the method.

� In other words , the message is a instruction and the method is the implementation.

Encapsulation and Information Encapsulation and Information HidingHiding� Information hiding is the principle of

concealing the internal data and procedures of an object and providing an interface to each object in such a way as to reveal as little as possible about its to reveal as little as possible about its inner workings.

� In C++ there are very general encapsulation protection mechanism with public , private and protected members.

Encapsulation and Information Encapsulation and Information HidingHiding� Public members (member data and

member functions) may be accessed from anywhere.

� Private members are accessible only from � Private members are accessible only from within the class.

� Protected members are only accessed from sub classes.

Encapsulation and Information Encapsulation and Information HidingHiding� Often an object is said to encapsulate the

data and a program.

� This means that the user cannot see the inside of the object “capsule”, but can use the object by calling the object’s methods.the object by calling the object’s methods.

� Encapsulation or information hiding is a design goal of an object-oriented system.

� Rather than allowing an object direct access to another object’s data , a message is sent to the target object requesting information.

Encapsulation and Information Encapsulation and Information HidingHiding� Another issue is per-object or per-class

protection.

� In per-class protection, the most common form( e.g., ADA, C++, Eiffel), class form( e.g., ADA, C++, Eiffel), class methods can access any object of that class and not just the receiver.

� In per-object protection, methods can access only the receiver.

Encapsulation and Information Encapsulation and Information HidingHiding� An important factor in achieving

encapsulation is the design of different classes of objects that operate using a common protocol, or object’s user common protocol, or object’s user interface.

� This means a program can send a generic message and leave the implementation up to the receiving object.

Class HierarchyClass Hierarchy

� An object-oriented system organizes classes into a subclass-superclasshierarchy.

� At the top of the class hierarchy are the � At the top of the class hierarchy are the most general classes and at the bottom are the most specific.

� The sub class inherits all the properties and methods defined in its superclass.

Class HierarchyClass Hierarchy

InheritanceInheritance

� Inheritance is a property of object-oriented systems that allows objects to be built from other objects.

� It takes the advantage from the � It takes the advantage from the commonality of objects when constructing new classes.

� Inheritance is the relationship between the classes where one is parent class of another (derived) class.

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 already that we can build on what we already have and more, important, reuse what we already have.

� Inheritance allows us to share and reuse the behaviors and attributes.

InheritanceInheritance

Ford

Mustang Taurus Thunderbird

Dynamic InheritanceDynamic Inheritance

� Dynamic inheritance allows objects to change and evolve overtime.

� Since base classes provide properties and attributes for objects, changing the base classes changes the properties and classes changes the properties and attributes of a class.

� More specifically, dynamic inheritance refers to the ability to add, delete, or change parents from objects(or classes ) at runtime.

Multiple InheritanceMultiple Inheritance

� Some object-oriented systems permit a class to inherit its state(attributes) and behaviors from more than one superclass.

� This kind of inheritance is referred to as � This kind of inheritance is referred to as multiple inheritance.

� Multiple inheritance can pose some difficulties

Multiple InheritanceMultiple Inheritance

� Example: Several distinct parents can declare a member within a multiple inheritance hierarchy.

� One way of achieving benefits of multiple � One way of achieving benefits of multiple inheritance is to inherit appropriate class and then add an object of another class as an attribute.

PolymorphismPolymorphism

� Poly means “many” and morph means “forms”.

� It means objects that can take and assume many forms.

� Polymorphism means that the same operation may behave differently on

� Polymorphism means that the same operation may behave differently on different classes.

� For example, consider how driving an automobile with a manual transmission is different from driving a car with an automatic transmission.

PolymorphismPolymorphism

� It is the fundamental concept in OOS.

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

Object Relationships and Object Relationships and AssociationsAssociations� Association represents the relationship

between objects and classes.

� For examples: A pilot can fly planes

� Here can fly is a relationship between a � Here can fly is a relationship between a pilot and planes entities.

� Associations are bidirectional; that means they can be traversed in both directions, perhaps with different connotations.

Object Relationships and Object Relationships and AssociationsAssociations� A direction implied by a name is the

forward direction ; the opposite direction is the inverse direction.

� Can fly connects a pilot to certain � Can fly connects a pilot to certain airplanes.

� The inverse of can fly could be called flown by.

Object Relationships and Object Relationships and AssociationsAssociations� Another important issue in association is cardinality, which specifies how many instances of one class may relate to a single instance of an associated class.single instance of an associated class.

� Cardinality constraints the number of related objects and often is described as being “one” or “many”.

ConsumerConsumer--Producer AssociationProducer Association

� A special form of association is a consumer producer relationship, also known as a client server association or a use relationship.use relationship.

� The consumer-producer relationship can be viewed as one way interaction: One object requests the service of another object.

ConsumerConsumer--Producer AssociationProducer Association

� The object that makes request is the client or consumer, and the object that receive request or provide service is the producer or serverproducer or server

Print Server Item

Request for service

Aggregation and object Aggregation and object ContainmentContainment� All objects, except most basic ones, are

composed of and may contain other objects.

� For example Car object is composed of � For example Car object is composed of engine and engine is still composed of shafts and valves.

� Breaking down the objects into the object from which they are composed is called decomposition.

Aggregation and object Aggregation and object ContainmentContainment� Since each object has identity, one object

can refer to other objects.

� This is known as aggregation, where attribute can be object itself.attribute can be object itself.

Object and IdentityObject and Identity

� A special feature of object-oriented systems is that every object has its own unique and immutable identity.

� An object identity comes into being � An object identity comes into being when the object is created and continues to represent that object then on.

� The identity is never confused with another object’s identity even if the object is deleted.

Object and IdentityObject and Identity

� The identity name never changes even if the properties of the object change- its independent of the object’s state.

� In object oriented systems , object � In object oriented systems , object identity is implemented through some kind of object identifier(OID) or unique identifier(UID).

� An OID guarantees uniqueness of every identifier.

Static and Dynamic bindingStatic and Dynamic binding

� The process of determining (dynamically) at runtime which function to invoke is termed dynamic binding.

� Making this determination earlier, at � Making this determination earlier, at compile time, is called static binding.

� Static binding optimizes the calls.

� Dynamic binding occurs when polymorphism calls are issued.

Object persistenceObject persistence

� Object have lifetime.

� They are explicitly created and can exist for a period of time that , traditionally, has been the duration of the process in been the duration of the process in which they were created.

� From language perspective this characteristics is called persistence.

Meta ClassesMeta Classes

� Information about classes is called meta class.