140 likes | 170 Views
Object Oriented Programming in Java. Object Oriented Concepts. What is an Object?. An object is a software bundle of related variables and methods . Software objects are often used to model real-world objects you find in everyday life. State and Behavior.
E N D
Object Oriented Programming in Java Object Oriented Concepts Object Oriented Programming in Java (95-707) Object oriented Concepts
What is an Object? • An object is a software bundle of related variables and methods. • Software objects are often used to model real-world objects you find in everyday life Object Oriented Programming in Java (95-707) Object oriented Concepts
State and Behavior • The “State” of an object is represented by the values of its variables • The Behavior of an object is represented by its methods Object Oriented Programming in Java (95-707) Object oriented Concepts
Encapsulation • Not a new idea. Examples are: • DLLs in Windows • Procedure in Pascal • Functions in C • Important because of the need to: • keep system modular • promote information hiding Object Oriented Programming in Java (95-707) Object oriented Concepts
Controlling Access to Objects State and Behaviors • Java has a number of built-in mechanism to do so using simple keywords such as: • private • public • protected Object Oriented Programming in Java (95-707) Object oriented Concepts
What is a Message • Software objects communicate between each other by sending messages to each other. Object Oriented Programming in Java (95-707) Object oriented Concepts
Parts of a Message • Destination • object name • Name of behavior to exercise • object method • How to modify behavior and state • parameters Object Oriented Programming in Java (95-707) Object oriented Concepts
Why are messages so great? • Allows hiding of internal data structures • Allows hiding on internal process logic • Allows remote calls Object Oriented Programming in Java (95-707) Object oriented Concepts
What is a Class? • A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind • Different objects can have the same class • Cars, Vans, Trucks are all objects of the class automobile • Are automobiles different from vehicles? Object Oriented Programming in Java (95-707) Object oriented Concepts
Differences between Object and Classes • Source of confusion because of several definitions • In general: • Objects are instances of their respective class Object Oriented Programming in Java (95-707) Object oriented Concepts
What are the possible relationships between objects • “Is-a” relationship • implements the behavior of some type • “Inheritance” relationship • subclass of some other object • “Is made of” relationship • declares other objects Object Oriented Programming in Java (95-707) Object oriented Concepts
Inheritance • Key concept in OO programming • Very powerful • To be used very carefully • Java enforces a single inheritance model • can inherit State and Behavior from super class • can override or add to super class state and behavior Object Oriented Programming in Java (95-707) Object oriented Concepts
Interfaces • Very powerful • Difficult to understand where to use at first • To be used a lot • Allows to: • capture similarities between classes without artificially forcing a relationship Object Oriented Programming in Java (95-707) Object oriented Concepts
Modeling Example • Write an OO program that simulates a furnace, a thermostat, and a user. • Example behaviors and states: • First the thermostat gets the set point from the user. Then the thermostat get the current temperature from a thermometer. • Then the thermostat tests the current temperature against the set point. If the current temperature is above the set point, the thermostat does not to turn the furnace on (or turns it off if already on) • When the furnace is turned on it announces that it has been turned on. Object Oriented Programming in Java (95-707) Object oriented Concepts