2 What is an object ? Real-world objects: Software objects: All have state and behaviorBicycles have state: current gear, two wheels, number of gears...Bicycles have behavior: braking, accelerating, changing gears...Software objects:Also have state and behaviorMaintains its state in variablesImplements its behavior in methods
3 DefinitionAn object is a software bundle of variables and related methods.Software objects can:represent real-world objects, such as a bicyclerepresent abstract concepts, such as an "Event" object in a window system
4 EncapsulationAnything 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 ModularitySource code can be written and maintained independentlyAn object can be passed easily aroundInformation hidingAn 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 ExamplesWhen 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 MessagesA 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.
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 instance of the class of objects known as bicycles.Bicycles of the same model are built using the same blueprint.
11 Software objectsPossible 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 instantiationRemember: a blueprint of a bicycle is not really a bicycle.When you create an instance of 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 PolymorphismThe attribute that allows one interface to be used with a general class of actions.It is the compiler's job to select the specific action as 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 ExampleThree 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 InheritanceThe process by which one object can acquire the properties of another object.Concept of classificationRed Delicious apple ---> apple ---> fruit ---> foodAn object (class) need only define those qualities that make it unique.Allow classes to be defined in terms of other classes.
17 ExampleMountain bikes, racing bikes, and tandems are all different kinds of bicycles.These are subclasses of the bicycle class.The bicycle class is the superclass of these subclasses.
18 More on InheritanceEach subclass inherits instance variables from the superclass.Each subclass inherits methods from the superclass.Subclasses can add their own variables and methods.Subclasses can override inherited 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.