1 / 18

Object-oriented Programming Concepts

Object-oriented Programming Concepts. What is an object ?. Real-world objects: All have state and behavior Bicycles have state: current gear, two wheels, number of gears... Bicycles have behavior: braking, accelerating, changing gears... Software objects:

vwyatt
Télécharger la présentation

Object-oriented Programming Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-oriented Programming Concepts

  2. What is an object ? • Real-world objects: • All have state and behavior • Bicycles have state: current gear, two wheels, number of gears... • Bicycles have behavior: braking, accelerating, changing gears... • Software objects: • Also have state and behavior • Maintains its state in variables • Implements its behavior in methods

  3. Definition • An object is a software bundle of variables and related methods. • Software objects can: • represent real-world objects, such as a bicycle • represent abstract concepts, such as an "Event" object in a window system

  4. Encapsulation • Anything that an object does not know or cannot do is excluded from the object. • Encapsulation is used to hide unimportant implementation details from other objects. • Implementation details of an object can be changed at any time without affecting other parts of the program. • Definition:Packaging an object's variables within the protective custody of its methods is called encapsulation.

  5. Benefits of Encapsulation • Modularity • Source code can be written and maintained independently • An object can be passed easily around • Information hiding • An object has a public interface for other objects to communicate with. • An object can maintain private information and methods that can be changed without affecting other objects.

  6. Examples • When you want to change gears on your bicycle, you don't need to know how the gear mechanism works, you just need to know which lever to move. • With software objects, you don't need to know how a method is implemented, you just need to know which method to invoke and its interface.

  7. Messages • A single object is a component of a larger program that contains many other objects. • Application functionality is achieved by interactions of these objects. • Software objects interact and communicate with each other by sending messages to each other.

  8. Messaging

  9. Components of a message • The object to whom the message is addressed. (Your Bicycle) • The name of the method to perform. (changeGears) • Any parameters needed by the method. (lower gear)

  10. What are classes? • Your bicycle Your bicycle is just one of many bicycles in the world. • Bicycle have some state and behavior in common. • However, each bicycle's state is independent of and can be different from other bicycles. • Your bicycle object is an instanceof the class of objects known as bicycles. • Bicycles of the same model are built using the same blueprint.

  11. Software objects • Possible to have many objects of the same kind that share common characteristics. • Objects of the same kind can be built using the same blueprint. • Software "blueprints" for objects are called classes. • Definition: A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind.

  12. Object instantiation • Remember: a blueprint of a bicycle is not really a bicycle. • When you create an instanceof a class, you create an object of that type. • The system allocates memory for the instance variables declared by the class. • You can then invoke the object's instance methods to make it do something.

  13. Reusing Classes • Classes provide the benefit of reusability. • Programmers use the same class, and thus the same code, over and over again to create many objects.

  14. Polymorphism • The attribute that allows one interface to be used with a general class of actions. • It is the compiler's job to select the specific actionas it applies to each situation. • Polymorphism helps reduce complexity by allowing the same interface to be used to specify a general class of actions.

  15. Example • Three types of stacks, one for integer values, one for strings, and one for a specific structure. • You can create three sets of functions called push() and pop() , one for each type of data. • The general interface is that of pushing and popping data onto and from a stack. • When you push data on or pop from the stack, it is the type of the data that will determine which specific version of push() or pop(), that will be called.

  16. Inheritance • The process by which one object can acquire the properties of another object. • Concept of classification • Red Delicious apple ---> apple ---> fruit ---> food • An object (class) need only define those qualities that make it unique. • Allow classes to be defined in terms of other classes.

  17. Example • Mountain bikes, racing bikes, and tandems are all different kinds of bicycles. • These are subclassesof the bicycle class. • The bicycle class is the superclassof these subclasses.

  18. More on Inheritance • Each subclass inherits instance variables from the superclass. • Each subclass inherits methods from the superclass. • Subclasses can addtheir own variables and methods. • Subclasses can overrideinherited methods. • The class hierarchy can be as deep as needed. • Methods and variables are inherited down through the levels. • The further down in the hierarchy a class appears, the more specialized its behavior.

More Related