Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes.

Similar presentations


Presentation on theme: "OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes."— Presentation transcript:

1 OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes. –Position, course, speed. –Account name, balance, transactions. Attributes defined by fields in a class.

2 OOP with Java, David J. Barnes Defining Classes2 State and Complexity (cont.) More attributes mean potentially more complex states. –Attribute values are often interdependent. Radio wave band and valid frequency range. Try to keep class definitions as simple as possible. –Break up large classes into smaller collaborating classes.

3 OOP with Java, David J. Barnes Defining Classes3 The Outline of a Class Definition // Define a class called ClassName. class ClassName { // Method definitions go here.... // Field definitions go here.... } Class header and class body. Member definitions in the body. –Methods and fields.

4 OOP with Java, David J. Barnes Defining Classes4 Methods and Fields Methods and fields may be arranged in any order. –We tend to list methods before fields. Methods describe the behavior of instances. Fields are variables. –Instance variables (most common). –Class variables.

5 OOP with Java, David J. Barnes Defining Classes5 Instance and Class Variables Each instance (object) has its own copy of the instance variables. –speed attribute of Ship. –Alteration in one instance does not affect another’s. All instances share a class variable. –Altered via one instance, changed in all.

6 OOP with Java, David J. Barnes Defining Classes6 Using the SimpleNote Class class NoteMain1 { public static void main(String[] args){ // Create two new SimpleNote objects // ready for messages. SimpleNote milk = new SimpleNote(), swimming = new SimpleNote(); // ‘Write’ a message on one note. milk.setMessage("We need more milk."); // Check the message. System.out.println(milk.getMessage()); }

7 OOP with Java, David J. Barnes Defining Classes7 Defining Attributes // A class to represent a sticky note, that // could be stuck to a wall, door, // refrigerator, etc. This simplified version // just stores the message and provides access // to it. class SimpleNote { // Omit the methods for simplicity.... // The variable used to store the text. // The note is blank to start with. private String message = ""; }

8 OOP with Java, David J. Barnes Defining Classes8 Methods of the SimpleNote Class class SimpleNote { // Return what the message is. public String getMessage(){ return message; } // Change the message. public void setMessage(String m){ message = m; } // The variable used to store the text. // The note is blank to start with. private String message = ""; }

9 OOP with Java, David J. Barnes Defining Classes9 Common Types of Methods getMessage is an accessor method. –It returns the value of its associated attribute. –It has a return type, matching the type of the attribute. setMessage is a mutator method. –It changes the value of its associated attribute. –It has a formal argument, matching the type of the attribute.

10 OOP with Java, David J. Barnes/E. Jul Defining Classes10 Method Results Accessor methods have a return type: –public String getMessage() Methods with a return type have a return statement in their bodies: –return message; The object or value following return is passed back to the method caller.

11 OOP with Java, David J. Barnes Defining Classes11 Scope Fields of a class have class scope (at least). –They are freely accessible from anywhere in the class. We generally choose to manipulate an attribute only through its associated accessor and mutator. –This convention is not followed by all authors.

12 OOP with Java, David J. Barnes Defining Classes12 Type Compatibility The expression type in a return statement must match the return type of the method. The type of an actual argument must match the type of the formal argument. The type of the right-hand side of an expression must match the variable’s type on the left-hand side.

13 OOP with Java, David J. Barnes Defining Classes13 Primitive Types Major primitive types: –boolean, char, double, int. Minor primitive types: –byte, float, long, short. Primitive-types have no methods. String is not a primitive type, but occurs frequently.

14 OOP with Java, David J. Barnes Defining Classes14 The SimpleSwitch Class // A class with a boolean attribute. // A simple on/off switch. class SimpleSwitch { // Method definitions go here.... // Whether the switch is on or off. // The switch is off by default. private boolean on = false; }

15 OOP with Java, David J. Barnes Defining Classes15 The Public Interface of a Class class SimpleSwitch { // Turn the switch on. public void switchOn(){... } public void switchOff(){... } public boolean isTheSwitchOn(){ }... }

16 OOP with Java, David J. Barnes Defining Classes16 Bridging Methods class SimpleSwitch { // Turn the switch on. public void switchOn(){ setOn(true); }... private boolean getOn(){ return on; } private void setOn(boolean o){ on = o; } private boolean on = false; }

17 OOP with Java, David J. Barnes Defining Classes17 Review The public interface of a class should reflect real-world usage. –e.g., switchOn, switchOff. Extra methods required to bridge between public interface and private implementation. –Accessors and mutators may be sufficient. An object may invoke its own methods.

18 OOP with Java, David J. Barnes Defining Classes18 Encapsulation Safe guarding the state of an object is of primary importance. –Access to a bibliophile’s books. Private attributes can be guarded by an object. Access can be denied or granted to objects of other classes, as required.

19 OOP with Java, David J. Barnes Defining Classes19 Encapsulation (cont.) Java does not enforce privacy by default. –Use private designation for all attributes. Control over attribute values may be important because of knock-on effects. –The valid range for an attribute may change as other elements of the state are changed. –Chemical plant process control. –Aircraft orientation.

20 OOP with Java, David J. Barnes Defining Classes20 Accessor Methods Potentially provide outside access to a private attribute. –public accessor available from all other classes. –public accessor harmless for primitive type attributes. –private accessor limits availability to other objects of the same class. Keeps implementation details hidden.

21 OOP with Java, David J. Barnes Defining Classes21 Mutator Methods Necessary for external modification of a private attribute. More care required with visibility. –Public mutator potentially allows objects of all other classes to alter the state. –Method body could make checks to ensure validity of mutation.

22 OOP with Java, David J. Barnes Defining Classes22 General Form class C {... public type getField(){ return field; } private void setField(type f){ field = f; } private type field; }

23 OOP with Java, David J. Barnes Defining Classes23 Constructors - Initializing State An object should start its existence in a valid and consistent state. Constructors are used to achieve this aim. A constructor is never used to receive messages from other objects. –Invoked automatically as part of an object’s creation.

24 OOP with Java, David J. Barnes Defining Classes24 A No-Arg Constructor class SimpleController { public SimpleController(){ setTemperature(293.0); }... public void setTemperature(double t){ temperature = t; } private double temperature; }

25 OOP with Java, David J. Barnes Defining Classes25 Constructors with Arguments class Controller { public Controller(){ double defaultT = 293.0; setTemperature(defaultT); } public Controller(double initialT){ setTemperature(initialT); }... }

26 OOP with Java, David J. Barnes Defining Classes26 Creating Controllers class ControllerMain3 { public static void main(String[] args){ // Create controllers using the two // different constructors. Controller control1 = new Controller(), control2 = new Controller(275.0);... }

27 OOP with Java, David J. Barnes Defining Classes27 Overloaded Constructors - this class Controller { public Controller(){ // this must be the first statement. this(293.0); } public Controller(double initialT){ setTemperature(initialT); }... }

28 OOP with Java, David J. Barnes Defining Classes28 SimpleNote Class Constructor class SimpleNote { // Default constructor. public SimpleNote(){ setMessage("Blank Note"); } // Constructor with initial note. public SimpleNote(String m){ setMessage(m); } // The variable used to store the text. private String message; }

29 OOP with Java, Eric JulDefining Classes29 Creating SimpleNote class SimpleNoteMain { public static void main(String[] args){ // Create controllers using the two // different constructors. SimpleNote mynote1 = new SimpleNote(), mynote2 = new SimpleNote( " Fun! " );... mynote1 = new SimpleNote( "More Fun");... mynote1.setMessage(“Still fun? “);... }

30 OOP with Java, David J. Barnes Defining Classes30 Identifier Usage Identifiers used, so far, for: –Names of classes. –Formal argument names. –Method names. –Constructor names. –Method variable names. –Field names.

31 OOP with Java, David J. Barnes Defining Classes31 Name Resolution Inside a method, look for a matching method variable or formal argument. Look for a matching method or field within the class. Look for another class in the current context (current package or imported package). Look for a default class (e.g., String ).

32 OOP with Java, David J. Barnes Defining Classes32 Review Class definitions capture the behavior and attributes of typical objects. Behavior modeled by method definitions. Attributes modeled by field definitions. –Privacy helps to encapsulate object state. Accessor and mutator methods provide access to object state.

33 OOP with Java, David J. Barnes Defining Classes33 Review (cont.) Accessor and mutator methods can be written to follow a common pattern. Object initialization is provided through constructors. –Constructors may take arguments. The meaning of an identifier depends upon the context in which it is used.


Download ppt "OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes."

Similar presentations


Ads by Google