Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fields, Constructors, Methods. Outline Creating components of a class –Fields –Constructors –Methods Encapsulation / visibility modifiers Practice: Thermostat.

Similar presentations


Presentation on theme: "Fields, Constructors, Methods. Outline Creating components of a class –Fields –Constructors –Methods Encapsulation / visibility modifiers Practice: Thermostat."— Presentation transcript:

1 Fields, Constructors, Methods

2 Outline Creating components of a class –Fields –Constructors –Methods Encapsulation / visibility modifiers Practice: Thermostat

3 Basic class structure Three major components of a class: –Fields – store data for the object to use –Constructors – allow the object to be set up properly when first created –Methods – implement the behavior of the object public class ClassName { Fields Constructors Methods }

4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fields Fields store values for an object. They are also known as instance variables. Fields define the state of an object. public class Square { private int x; private int y; private int size; private Color fillColor; // Further details omitted. } private int size; visibility modifier type variable name

5 Data Types boolean char int long float double String Student (any class) — true, false — a…z,A…Z,[symbols] — integers — larger integers (2x) — floating decimal — larger floating decimal (2x) — characters strung together — defined class

6 Constructors Constructors initialize an object. They have the same name as their class. They store initial values into the fields. They often receive external parameter values for this. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public Square() { x = 0; y = 0; size = 0; fillColor = Color.blue; }

7 /** * Gets the size of the square. */ Methods public int getSize() { return size; } return type method name parameter list (empty) start and end of method body (block) return statement visibility modifier method header/signature

8 /** * Sets the size of the square. */ Methods public void setSize(int s) { size = s; } return type method name parameter list (empty) start and end of method body (block) return statement visibility modifier method header/signature

9 Components are distinct Fields [visibility modifier] [data type] [field name]; Constructor [visibility modifier] [name of class] ([parameter list]) { // constructor body } Methods [visibility modifier] [return type] [method name] ([parameter list]) { // method body }

10 Encapsulation We can take one of two views of an object: –internal - the details of the variables and methods of the class that defines it –external - the services that an object provides and how the object interacts with the rest of the system From the external view, an object is an encapsulated entity, providing a set of specific services These services define the interface to the object Copyright © 2012 Pearson Education, Inc.

11 Encapsulation One object (called the client) may use another object for the services it provides The client of an object may request its services (call its methods), but it should not have to be aware of how those services are accomplished Any changes to the object's state (its variables) should be made by that object's methods We should make it difficult, if not impossible, for a client to access an object’s variables directly That is, an object should be self-governing Copyright © 2012 Pearson Education, Inc.

12 Encapsulation An encapsulated object can be thought of as a black box – its inner workings are hidden from the client The client invokes the interface methods and they manage the instance data (i.e., fields) Methods Data Client Copyright © 2012 Pearson Education, Inc.

13 Visibility Modifiers In Java, we accomplish encapsulation through the appropriate use of visibility modifiers A modifier is a Java reserved word that specifies particular characteristics of a method or data We've used the final modifier to define constants Java has three visibility modifiers: public, protected, and private The protected modifier involves inheritance, which we will discuss later Copyright © 2012 Pearson Education, Inc.

14 Visibility Modifiers Members of a class that are declared with public visibility can be referenced anywhere Members of a class that are declared with private visibility can be referenced only within that class Members declared without a visibility modifier have default visibility and can be referenced by any class in the same package An overview of all Java modifiers is presented in Appendix E Copyright © 2012 Pearson Education, Inc.

15 Visibility Modifiers (Implications & Use) Public variables violate encapsulation because they allow the client to modify the values directly Therefore instance variables should not be declared with public visibility It is acceptable to give a constant public visibility, which allows it to be used outside of the class Public constants do not violate encapsulation because, although the client can access it, its value cannot be changed Copyright © 2012 Pearson Education, Inc.

16 Methods that provide the object's services are declared with public visibility so that they can be invoked by clients Public methods are also called service methods A method created simply to assist a service method is called a support method Since a support method is not intended to be called by a client, it should be declared with private visibility Copyright © 2012 Pearson Education, Inc. Visibility Modifiers (Implications & Use)

17 Visibility Modifiers publicprivate Fields Methods Provide services to clients Support other methods in the class Enforce encapsulation Violate encapsulation Copyright © 2012 Pearson Education, Inc.

18 Let’s design a Thermostat class What information is needed (fields) –int min, max –int currentTemp –boolean heat, cool What should its initial state be (constructor) –min = 60, max = 75 –currentTemp = 72 –heat and cool are off What should we be able to do (methods) –updateDisplay (private ?) –turnOnHeat, turnOffHeat (similar for cool) –incMax, decMax (similar for min) Copyright © 2012 Pearson Education, Inc.

19 updateDisplay Show the current state of the thermostat: Current temp:72 Max temp:75 Min temp:60 Cool:true Heat:false

20 Homework CodingBat exercises Read 3 & 4 Try to Finish Labs 3 and 4


Download ppt "Fields, Constructors, Methods. Outline Creating components of a class –Fields –Constructors –Methods Encapsulation / visibility modifiers Practice: Thermostat."

Similar presentations


Ads by Google