Download presentation
Presentation is loading. Please wait.
1
Implementing Classes Chapter 3
2
What is a Java Class? A Java class describes the behaviors and attributes of an object. There are at least two java classes associated with a Java project. A class implementation .java file. This class contains instance variables and methods for accomplishing a task. A tester class .java file. The tester class instantiates an object of the class and calls the member functions described in the implementation class.
3
Syntax of a Java Class Declaration
public class ClassName { Instance variables - data or information an object contains visible by the methods of a class. accessibility data type variable name; private double balance; Constructor methods() }
4
Accessibility Accessibility refers to the information or data exposure to other classes. public variables are exposed to all classes private variables are only exposed to classes to which they belong.
5
Method Return Types A method return type refers to how a method performs an action. A void method performs an action without storing a value. A void method does not require a return statement public void greeting()//this method simply displays text output. It does not store the outcome of a computation. { System.out.println(“Hello!”); }
6
Methods that return values
pubic int getValue()//this method will return the information contained in a class object. { return value; Return statement }
7
Syntax of declaring class methods
public return type method name (parameter list) {accessibility identifies the method data type of the method results.(void, int, double, string, etc) arguments- information a method needs to perform a task }
8
Example of a Java implementation class
//This class will simulate pressing a counting device. public class Clicker{ private int value // an instance variable to store the number of clicks. public void click()// a mutator method to add 1 each time the method is called { value = value +1; } }
9
Clicker class continued….
//This method accesses the number of times the click method has been called. public int getValue() { return value; }
10
Clicker class continued...
//a mutator method to reset the number of clicks stored in value back to 0. //This method does not return a value. public void reset() { value=0; }
11
Tester Class //This class will instantiate an object of the Clicker class and call the methods of the class. public class ClickerTester{ int numClicks(); Clicker myClicker = new Clicker(); //instantiate an object of a Clicker class clas Object myClicker.click() // the object calls the click method. //The numClicks variable is increased by 1; System.out.println(myClicker.getValue());//displays the value of the numClicks variable myClicker.click()// another method call to the click method }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.