Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fields, Constructors, Methods

Similar presentations


Presentation on theme: "Fields, Constructors, Methods"— Presentation transcript:

1 Fields, Constructors, Methods
Think, Pair, Share – what is the difference between a class and an object? What are some of the classes we’ve used so far?

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

3 Objects First with Java
Basic class structure public class ClassName { Fields Constructors Methods } 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 © David J. Barnes and Michael Kölling

4 Objects First with Java
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. } type visibility modifier variable name private int size; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

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 Objects First with Java
Constructors public Square() { x = 0; y = 0; size = 0; fillColor = Color.blue; } 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. Only here as a brief introduction and to reinforce what they saw last class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

7 Objects First with Java
Methods /** * Gets the size of the square. */ method header/signature return type visibility modifier method name parameter list (empty) public int getSize() { return size; } return statement Only here as a brief introduction start and end of method body (block) © David J. Barnes and Michael Kölling

8 Objects First with Java
Methods /** * Sets the size of the square. */ method header/signature return type visibility modifier method name parameter list (empty) public void setSize(int s) { size = s; } return statement Only here as a brief introduction start and end of method body (block) © David J. Barnes and Michael Kölling

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 I put up a black/blank screen and reviewed the pieces of each with them. You do NOT put them inside of each other, they are all at the class level!

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 Visibility Modifiers (Implications & Use)
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.

17 Visibility Modifiers public private Violate encapsulation Enforce
Fields Methods Violate encapsulation Enforce encapsulation Support other methods in the class Provide services to clients 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) Setting up the class like this will show how it is important to make field private and have methods to change state AND update the display and/or other fields 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"

Similar presentations


Ads by Google