340 likes | 354 Views
Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr. Title Slide. CSC 444. A strategy for organizing systems as collections of interacting objects that combine data and behavior. Object Oriented Methodology Object Oriented Design Object Oriented Programming
E N D
Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr. Title Slide CSC 444
A strategy for organizing systems as collections of interacting objects that combine data and behavior. Object Oriented Methodology Object Oriented Design Object Oriented Programming Object Oriented Testing What Is Object Orientation?
Procedural Method: Developer concentrates on developing code that mimics the procedure to represent the real world system. Object Oriented Method: Developer concentrates on developing representations of real world objects, their actions, and how they relate to each other. Developing Programs
An object is a software “bundle” of data and procedures that act on the data. A procedure is called a “method”. Objects are the heart of OOM. Objects share two common characteristics State: What an object is currently “doing” Behavior: What can an object do Objects
A Lion: States Color Weight Tired? Hungry? Behaviors Sleep Hunt Roar Example Of Objects
A Car States Current Speed Type of Transmission 2/4 Wheel Drive Lights On Current Gear Behaviors Turning Breaking Accelerating Another Object
A Baseball Player States Batter Pitcher Behaviors Batting Average Earned Run Average One More
Pictorial View Of A Class Message Response Methods Data
An object is an instance of a class. Classes must be instantiated to produce objects. Examples: FordMustang = new Car ChevyCorvette = new Car Each object usually has different values for its instance variables. Objects
The Car Object Break Accelerate Steer Change Gears Toggle Lights Methods 75 mph 4th Gear Lights On Data
To create an individual object, a class must be instantiated. Example: myCar=new Car (75mph, 4thgear, lightson) yourCar=new Car (60mph, 1stgrear, lightsoff) Use the methods to manipulate the instance variables. myCar.Accelerate(+15) yourCar.Accelerate(-10) Creating Objects
The process of packaging an object’s data and methods together. Objects consist of a public interface (external) and a private (internal) section. The internal portion has more limited visibility than the external portion. Safeguards the object against unwarranted external access. Can only communicate with the object through its interface. Encapsulation
Implementation Hiding : Protection of the internal implementation of object. The internal data and methods are the parts of the object hidden from other objects. Major benefit: Pata and methods can be changed without affecting other objects. Modularity: An object can be maintained independently of other objects. Also easier to distribute objects through the system. Benefits Of Encapsulation
How software objects interact and communicate with each other. Analogy: Two people communicating with each other. Person 1 to person 2: “Come here, please”. Sometimes the message must contain additional information. This is done through message parameters. Messages
Message: Accelerate the car by 15 mph. The object to receive the message: car The name of the action to perform: accelerate Any parameters the method requires: 15 mph Message passing is accomplished through method calling. Messages and methods are synonymous. Messages can be sent to objects in another location: Distributed Object Example
A class is a template (or prototype) for an object. Analogy: A class is to an object is what a blueprint is to a house -- many houses can be built from the same blueprint. A blueprint outlines the makeup of a house. An object is an instantitation of a class. Classes can not be used by themselves. There may be many different instances of a class. Many cars, many lions, etc. Classes
Benefits of objects: modularity and information hiding. Benefits of classes: reusability. Each instance of a class has it’s own state variables, but share methods. State variables of the instance of a class are called “instance variables”. Classes (ctd)
What happens if an object is basically the same as other objects, but it has a few slight differences? Inherit the basic characteristics from a super class and form a sub class (AKA: parent-child classes). Inheritance: The process of creating a new class with the characteristics of an existing class, along with additional characteristics unique to the new class. Inheritance
When a subclass inherits from a superclass, it inherits all of the superclass state variables and the methods. Additional methods and/or state variables can be added to the subclass. Inheritance (ctd)
Class: car Basic data Number of wheels Number of doors Types of Cars Gas powered Fuel tank capacity MPG Electric Powered Size battery Example
Inherited methods can be overridden Different code can be supplied in the methods for each subclass. Example: Gas powered cars can accelerate faster than electric powered cars, so the acceleration routine might be different. Inherited Methods
Class: Figure Rectangle State Variables: Length, Width Methods: Calculate_Area Circle: State Variables: Radius Method: Calculate_Area Another Example
Inheritance Tree Car Gas Powered Electric Powered 4-Cylinder 6-Cylinder
Sometimes it is useful to create superclasses that act purely as templates for more subclasses. The superclass serves as nothing more than an abstraction for the common class functionality shared by its subclasses. Called abstract classes. Abstract classes can not be instantiated - an object can not be created from it. Abstract Classes
Reason: Parts of it are yet to be defined. Parts of methods have yet to be implemented - abstract methods. The actual implementation of the method is defined in the subclass. Abstract Classes (ctd)
Acceleration method can not be defined until the acceleration capabilities are known. How a car accelerates is determined by the type of engine it has. Engine type is unknown in the superclass Child class would implement the specific acceleration method to reflect the acceleration capabilities of their respective engines. Example
Class: Baseball Player Subclasses: Batter and Pitcher All baseball players have some basic data: Team, Uniform#, Birthdate, etc. All baseball players have some basic methods: ComputeAge, TaxComputations, etc. Another Example
Batters have some specific data: Atbats, Hits, Homeruns, etc. Batters have some specific methods: CalculateBattingAverage, CalculateSluggingPercentage, Etc. Pitchers have some specific data: NumbOfWins, NumbOfLosses, InningsPitched, EarnedRuns, etc. Pitchers have some specific methods: CalculateERA, etc. Another Example (ctd)
Example (ctd) Baseball Player Class Player Name, Team Name Uniform#, Birthdate CalculateAge CalculateTaxes Batter Subclass Pitcher Subclass NumberOfWins, NumberOFLosses, InningsPitched, EarnedRuns CalculateERA AtBats, Hits, HomeRuns CalculateBattingAverage, CalculateSP
It doesn’t make sense to create (instantiate) a Baseball Player object from the Baseball Player class (the abstract class). We must create an object of the subclasses, Batter or Pitcher (which inherits from the super class Baseball Player) Example (ctd)
Enables a subclass to inherit characteristics from more than one superclass. Example: A whale inherits some characteristics from mammals and some from fish. Multiple Inheritance
Means “having many forms”. Describes an elegant and relative simple technique by which a reference that is used to invoke a method can actually invoke different methods at different times, depending upon the nature of the invocation. Accomplished by late binding of the class type to the method. Polymorphism
Assume a superclass of “figure” which has a method called computeArea. Assume several subclasses of “figure”. Examples: Square, Triangle, Circle, etc. The method of computing the area of a figure is different for each different type of figure. Example
Since Square, Triangle, etc. are subclasses of figure, they can be assigned into figure. The method computeArea can be passed the value figure. figure = square (associated data) computeArea (figure) The correct method for computing the area is then executed because of late binding (binding at run time). Example (ctd)