Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Overview of Object-Oriented Software Design and Java Programming Putting the Pieces Together!

Similar presentations


Presentation on theme: "1 Overview of Object-Oriented Software Design and Java Programming Putting the Pieces Together!"— Presentation transcript:

1 1 Overview of Object-Oriented Software Design and Java Programming Putting the Pieces Together!

2 2 Object-Oriented Design A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data.

3 3 Object Oriented Programming l Programmer thinks about and defines the attributes and behavior of objects. l Often the objects are modeled after real-world entities. l Very different approach than function-based programming (like C).

4 4 Reasons for OOP Abstraction Polymorphism Inheritance Encapsulation Software Engineering Issues

5 5 Objects to Classes A class defines the pattern used when instantiating an object of that type. A class generally contains private data and public operations (called methods).

6 6 Class: Object Types l A Java class is an object type. l When you create the definition of a class you are defining the attributes and behavior of a new type. n Attributes are data members. n Behavior is defined by methods.

7 7 Creating an object l Defining a class does not result in creation of an object. l Declaring a variable of a class type creates an object. You can have many variables of the same type (class). Instantiation

8 8 Superclass and Subclass Inheritance enables us to define a new class (called a subclass) that inherits the properties of an already existing class. The newly derived class is then specialized by adding properties specific to it. The class being inherited from is the superclass. The class that inherits properties is the subclass.

9 9 Defining Objects An object-oriented program consists of many objects. An object is composed of identity, state (attributes, data, and their current values) and behavior (operations).

10 10 Identity, State, Behavior Identity is the property of an object that distinguishes it from all other objects. The failure to recognize the difference between the name of the object and the object itself is the source of many errors in object-oriented (OO) programming.

11 11 Identity, State, Behavior The state of an object encompasses all of the (static) properties of the object plus the current (dynamic) values of each of these properties A property is an inherent or distinctive characteristic, trait, quality, or feature that contribute to making an object uniquely that object We will use the word attribute, or data member, to refer to the state of an object

12 12 Examples of State Properties Elevators travel up or down Vending machines accept coins Clocks indicate the current time Values Current floor Number of coins deposited The number of minutes since the last hour

13 13 Identity, State, Behavior Behavior is how an object acts and reacts, in terms of state changes and interactions with other objects. An operation is some action that one object performs upon another in order to elicit a reaction. We will use the word method to describe object behavior in C++. Invoking a method causes the behavior to take place.

14 14 Classes Classes are the definitions (or blueprints) used to create objects. I’d say: descriptions of objects. To make a car the manufacturer must first have a design from which to build the first car. Then, once all the problems are worked out, the design is used to build all the cars of that model.

15 15 Objects An object is an instance of a class. If we have a class definition called Car, then we can think of Audi, BMW, and Corvette as each being an instance (object) of the class Car, i.e., they are each a type of car.

16 16 Object example Audi 6BMW Z3Corvette Notice that all objects are of the same type. All objects are cars! Car

17 17 Classes and Objects An object is an instance of exactly one class Corvette can not be an instance of a car class and an instance of a plane class at the same time. An instance of a class, an object, belongs to that particular class. A Corvette is a car  Corvette belongs to the class Car.

18 18 Classes Once a class is defined you can create as many instances of the class (objects from the class) as you would like. Once a blue print is completed for the 2004 Porsche 911, Porsche will use an assembly line to build as many instances of the 2004 Porsche 911 as they wish.

19 19 Defining a class Properties are variables which describe the essential characteristics of an object. Properties of a car: color, model, make, how many doors, transmission type, direction of movement, etc. Behaviors are methods that describe how the object behaves and how the properties may be modified. Behavior of a car: braking, changing gears, opening doors, moving forwards or backwards, etc.

20 20 Instance variables The class definition will include parameter definitions (properties) that represent data about a particular object, instance variables. Example, Joe's car may have 4 gallons of gas in it while John's car has 10 gallons. The amount of gas in each car may change without affecting the amount of gas in the any other cars. All instances (objects) of a class will have a set of instance variables that are specific to that individual object. The combination of the values of these instance variables is known as the object’s state.

21 21 Instance variables Audi 6BMW Z3Corvette Car MaxSpeed = 155MaxSpeed = 165MaxSpeed = 145 MaxSpeed

22 22 Class variables The class definitions may also include parameter definitions that represent data that is shared by all class instances (objects), called class variables. In the case of the car class, we will define a maximum allowed speed, by the law (variable MaxSpeed). This will be the same for each individual car.

23 23 Class variables Audi 6BMW Z3Corvette Car MaxSpeed = 155MaxSpeed = 165MaxSpeed = 145 MaxSpeed MaxSpeed=155

24 24 Class variables Class variables may also be used to keep track of things such as how many instances of a class exist. Example: let’s create a counter the records how many cars are in the garage.

25 25 Class variables Audi 6BMW Z3Corvette Car MaxSpeed = 155MaxSpeed = 165MaxSpeed = 145 MaxSpeed MaxSpeed=155 NumCars = 3

26 26 Messages For Objects The object to whom the message is being sent. The name of the method that object is to execute. Any parameters (variables) needed by that method. For Humans Who the message is for. What we want the person to do. What information is needed to do it. Audi 6 turnOnHazard()

27 27 Messages and Methods In order to process a message, an object needs to have a method defined for the requested task. A method is a small, well-defined piece of code that completes a specific task. For our previous example, we need to define a method to turn on the car's hazard lights.

28 28 Messages and Methods Audi 6BMW Z3Corvette Car MaxSpeed = 155 turnOnHazard() MaxSpeed = 165 turnOnHazard() MaxSpeed = 145 turnOnHazard() MaxSpeed MaxSpeed=155 NumCars = 3 turnOnHazard()

29 29 Instance methods Each class can have methods that are specific to each object, called instance methods. These can only affect that object's parameters, i.e., it’s instance variables. Example: If BMW has 4 gallons of gas and someone puts 6 more gallons of gas in his/her car, the car now has 10 gallons. The amount of gas in Audi and Corvette is unchanged.

30 30 Messages and Methods Audi 6BMW Z3Corvette Car MaxSpeed = 155 turnOnHazard() addGass(amount) MaxSpeed = 165 turnOnHazard() addGass(amount) MaxSpeed = 145 turnOnHazard() addGass(amount) MaxSpeed MaxSpeed=155 NumCars = 3 turnOnHazard() addGass(amount)

31 31 Methods It is also possible that you want information from an object; in this case, you would define a method that sends (returns) a message back to the requester containing that information. We need to know how much gas is in our cars, so we will create a new method that returns the value of Gas-Level variable for our car.

32 32 Messages and Methods Audi 6BMW Z3Corvette Car MaxSpeed = 155 GasLevel = 4 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed = 165 GasLevel = 10 MaxSpeed = 145 GasLevel = 6 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed GasLevel MaxSpeed=155 NumCars = 3 addGass(amount)getGasLevel():GasLevel turnOnHazard() addGass(amount)getGasLevel():GasLevel turnOnHazard()

33 33 Class methods Class methods are used to get or manipulate information about all objects created from the class. Typically, class methods are changing class variables. For example: Each time we move the car in or out of the garage, we need to add/subtract one to the number of cars: carIn( ) & carOut( ) Also, we may want to know how many cars are actually in the garage: getNumCars( )

34 34 Messages and Methods Audi 6BMW Z3Corvette Car MaxSpeed = 155 GasLevel = 4 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed = 165 GasLevel = 10 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed = 145 GasLevel = 6 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed GasLevel MaxSpeed=155 NumCars = 3 addGass(amount)getGasLevel():GasLevel turnOnHazard() carIn() carOut() getNumCars():NumCars

35 35 Object Oriented Programming When writing object-oriented programs, first one must define the classes (like Car). Then, while the program is running, the instances of the classes (objects) (such as Audi, BMW, Corvette in our example) are created.

36 36 Object Oriented Programming - Benefits An object can be written and maintained separately from the rest of the program, modularity. An object has a “public face” that it uses to communicate with other objects, but other objects can not directly access its instance variables, information hiding.

37 37 Information Hiding l The interface to a class is the list of public data members and methods. l The interface defines the behavior of the class to the outside world (to other classes and functions that may access variables of your class type). l The implementation of your class doesn't matter outside the class – only the interface.

38 38 Information Hiding (cont.) l You can change the implementation and nobody cares! (as long as the interface is the same). l You can use other peoples classes without fear!

39 39 Polymorphism The ability of different objects to respond to the same message in different ways.

40 40 Inheritance l You can create a new class that inherits from an existing class. l You can add new members and methods. l You can replace methods. l The new class is a specialization of the existing class.

41 41 Inheritance All classes in Java are organized into a class hierarchy. The highest level classes are very general and the lower level classes are more specific. The lower level classes are based upon the higher level classes and inherit instance variables and methods from those higher level class. They also may contain their own (new) instance variables and methods beyond the higher level class definition.

42 42 Inheritance A higher level class is called a superclass; a lower level class is called a subclass. A subclass may also be a superclass Inheritance allows you to define certain behaviors once and then to reuse those behaviors over and over again in the subclasses. This is called reusability.

43 43 Inheritance Example l Base class is shape, represents the abstract notion of a shape. l Derived classes: n rectangle n circle n triangle. l An object that is a circle is also a shape!

44 44 Inheritance Example Our Car class is very general. Let's define a new class called BMW that contains the parameters: model, color, engine size.

45 45 Inheritance Car MaxSpeed GasLevel MaxSpeed=155 turnOnHazard() addGass(amount) getGasLevel():GasLevel BMW Model Color EngineSize MaxSpeed GasLevel turnOnHazard() addGass(amount) getGasLevel():GasLevel

46 46 Inheritance Now let's define two new classes. One for the Z3 and another for the 3 Series Sedan. What might be some of the differences between the two classes? Number of doors (3, 5) Roof (soft or hardtop) Therefore, we add variables NumDoors and Roof

47 47 Inheritance Car MaxSpeed GasLevel MaxSpeed=155 turnOnHazard() addGass(amount) getGasLevel():GasLevel BMW Model Color EngineSize MaxSpeed GasLevel turnOnHazard() addGass(amount) getGasLevel():GasLevel Z3 Model Color EngineSize Roof MaxSpeed GasLevel turnOnHazard() addGass(amount) getGasLevel():GasLevel 3 series Model Color EngineSize NumDoors MaxSpeed GasLevel turnOnHazard() addGass(amount) getGasLevel():GasLevel

48 48 Views of the class A class can be viewed as a sort of contract that specifies what instances of the class can, and cannot do It is possible to distinguish between the outside and inside view of a class The interface of a class provides its outside view and emphasizes the abstraction The implementation of a class is its inside view

49 49 Access Most classes provide three levels of access to their members (state and behavior): Public The part of the class that is visible to all clients of the class Protected The part of the class that is only visible to subclasses of the class Private A part of the class that is not visible to any other classes

50 50 Private vs. Public l Public data members and methods can be accessed outside the class directly. l The public stuff is the interface. l Private members and methods are for internal use only.

51 51 Protected Class members/methods l We've already seen private and public. l Protected means derived classes have access to data members and methods, but otherwise the members/methods are private.

52 52 Special Member Functions l Constructors: called when a new object is created (instantiated). n can be many constructors, each can take different arguments. l (C++) Destructor: called when an object is eliminated (not in Java) n only one, has no arguments.

53 53 Accessing Data Members l Data members are available within each method (as if they were local variables). l Public data members can be accessed by other functions using the member access operator "." (just like C++ struct).

54 54 Accessing class methods l Within other class methods, a method can be called just like a function. l Outside the class, public methods can be called only when referencing an object of the class.

55 55 static methods l A static method is a class method that can be called without having an object. l Method can't access non-static data members! (they don't exist unless we have an object).

56 56 Static Data Members l It is possible to have a single variable that is shared by all instances of a class (all the objects). n declare the variable as static. l Data members that are static must be declared and initialize outside of the class. n at global or file scope.

57 57 Static data member example class foo { static int cnt; foo() { foocount++; }...

58 58 Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT Operations Data

59 59 Object-Oriented Programming Language Features 1. Data abstraction 2. Inheritance of properties 3. Dynamic binding of operations to objects

60 60 OOP Terms Java Equivalents ObjectClass object or class instance Instance variablePrivate data member MethodPublic member function Message passingFunction call ( to a public member function )

61 61 What is an object? OBJECT Operations Data set of methods (public member functions) internal state (values of private data members)

62 62 Inheritance Hierarchy Among Vehicles vehicle wheeled vehicleboat bicyclecar four-door two-door Every car is a wheeled vehicle.

63 63 Inheritance l is a mechanism by which one class acquires (inherits) the properties (both data and operations) of another class l the class being inherited from is the Base Class (Superclass) l the class that inherits is the Derived Class (Subclass) l the derived class is then specialized by adding properties specific to it

64 64 Composition (or Containment) l is a mechanism by which the internal data (the state) of one class includes an object of another class

65 65 TimeCard Class TimeCard has a Time object Private data: hrs mins secs Punch Private data: id timeStamp Increment Set Print. TimeCard Write.

66 66 Constructor Rules for Derived Classes l at run time, the base class constructor is implicitly called first, before the body of the derived class’s constructor executes l if the base class constructor requires parameters, they must be passed by the derived class’s constructor

67 67 Order in Which Constructors are Executed Given a class X, l if X is a derived class its base class constructor is executed first l next, constructors for member objects (if any) are executed (using their own default constructors if none is specified) l finally, the body of X’s constructor is executed

68 68 End of Lecture


Download ppt "1 Overview of Object-Oriented Software Design and Java Programming Putting the Pieces Together!"

Similar presentations


Ads by Google