Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.

Similar presentations


Presentation on theme: "Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To."— Presentation transcript:

1 Chapter 3 – Implementing Classes

2 Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand the purpose and use of constructors To understand how to access instance fields and local variables To appreciate the importance of documentation comments

3 Black Box A black box is something that performs a task, but hides how it does it This is also called encapsulation Black boxes in a car: transmission, electronic control module, etc

4 Levels of abstraction: Software Design

5 Old times: computer programs only manipulated primitive types such as numbers and characters Gradually programs became more complex, vastly increasing the amount of detail a programmer had to remember and maintain

6 3.2 Designing the Public Interface to a Class Your task: Develop a BankAccount class First step: Define essential features Behavior of bank account (abstraction): deposit money deposit money withdraw money withdraw money get balance get balance

7 Designing #1: Methods Methods of BankAccount class: deposit deposit withdraw withdraw getBalance getBalance Support method calls such as the following: harrysChecking.deposit(2000);harrysChecking.withdraw(500);System.out.println(harrysChecking.getBalance()); Which methods are accessors? Mutators?

8 Designing: Method Definitions COMPONENTS: access specifier (Ex. public) return type (Ex. String or void) method name (Ex. deposit) list of parameters (Ex. amount for deposit) method body in braces {…}

9 Examples: public void deposit(double amount) {...} public void withdraw(double amount) {...} public double getBalance() {...}

10 Syntax 3.1: Method Definition accessSpecifier returnType methodName(parameterType parameterName,...) { method body }Example: public void deposit(double amount){... } public void deposit(double amount){... }Purpose: To define the behavior of a method

11 Designing #2: Constructor A constructor initializes the instance variables Constructor name = class name public BankAccount() { // body--filled in later }

12 Constructors Constructor body is executed when new object is created Statements in constructor body will set the internal data of the object that is being constructed How does the compile know which constructor to call? How does the compile know which constructor to call?

13 Constructor vs. Method Constructors are a specialization of methods Goal: to set the internal data of the object Goal: to set the internal data of the object 2 differences All constructors are named after the class All constructors are named after the class Therefore all constructors of a class have the same name No return type listed EVER! No return type listed EVER!

14 Syntax 3.2 : Constructor Definition accessSpecifier ClassName(parameterType parameterName,...) { constructor body } Example: public BankAccount(double initialBalance) {... }Purpose: To define the behavior of a constructor

15 BankAccount Public Interface The public constructors and methods of a class form the public interface of the class. public class BankAccount { // private fields--filled in later // Constructors public BankAccount() { // body--filled in later // Constructors public BankAccount() { // body--filled in later }

16 public BankAccount(double initialBalance) { // body--filled in later } // Methods // Methods public void deposit(double amount) { // body--filled in later } public void withdraw(double amount) { // body--filled in later } public double getBalance() { // body--filled in later }}

17 Remember Public methods and constructors provide the public interface to a class They are how you interact with the black box They are how you interact with the black box Our class is simple, but we can do many things with it Notice we haven’t defined the method body yet, but know how we can use it Notice we haven’t defined the method body yet, but know how we can use it

18 3.3 Commenting the Public Interface Part of creating a well-defined public interface is always commenting the class and methods behaviors The HTML pages from the API are created from special comments in your program called javadoc comments Place before the class or method /**… /**…....*/

19 Javadoc Comments Begin with /** Ends with*/ Put * on lines in between as convention, makes it easier to read Javadoc tags - @ mark is one @author, @param, @return @author, @param, @returnBenefits Online documentation (Assignment 1) Online documentation (Assignment 1) Other java documents Other java documents

20 Javadoc comments First sentence is extracted for HTML page Carefully explain method Carefully explain method @param for each parameter Omit if no parameters Omit if no parameters @return for the value returned Omit if return type void Omit if return type void

21 /** * Withdraws money from the bank account. * Withdraws money from the bank account. * @param the amount to withdraw * @param the amount to withdraw */ */ public void withdraw(double amount){ // implementation filled in later }/** * Gets the current balance of the bank * Gets the current balance of the bank * account. * account. * @return the current balance * @return the current balance */ */ public double getBalance(){ // implementation filled in later }

22 Class Comment /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount{... }

23 3.4 Instance Fields Remember: methods are the public interface of a class Instance Fields are part of the internal workings - An object stores its data in instance fields Field: a technical term for a storage location inside a block of memory Field: a technical term for a storage location inside a block of memory Instance of a class: an object of the class Instance of a class: an object of the class AKA Data Members AKA Data Members

24 The class declaration specifies the instance fields: public class BankAccount { private double balance;... }

25 An instance field declaration consists of the following parts: access specifier (usually private) access specifier (usually private) type of variable (such as double) type of variable (such as double) name of variable (such as balance) name of variable (such as balance) Each object of a class has its own set of instance fields You should declare all instance fields as private

26 Access Specifiers Access Specifiers – defines the accessibility of the instance field and methods private – only accessible within the class methods private – only accessible within the class methods public – accessible in outside class methods and inside class methods public – accessible in outside class methods and inside class methods Private enforces encapsulation/black box AKA Visibility Modifier

27 Syntax 3.4 : Instance Field Declaration Syntax 3.4 : Instance Field Declaration accessSpecifier class ClassName {... accessSpecifier fieldType fieldName;... }Example: public class BankAccount {... private double balance;... } Purpose: To define a field that is present in every object of a class

28 Accessing Instance Fields The deposit method of the BankAccount class can access the private instance field: public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; }

29 Other methods cannot: public class BankRobber { public static void main(String[] args) { BankAccount momsSavings = new BankAccount(1000);... momsSavings.balance = -1000; // ERROR } }

30 Encapsulation = Hiding data and providing access through methods By making data members private, we hide internal workings of a class from a user Note: We can have public instance fields and private methods, but commonly we do not

31 3.5 Implementing Constructors & Methods Constructors contain instructions to initialize the instance fields of an object public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; }

32 Constructor Call Example BankAccount harrysChecking = new BankAccount(1000); Create a new object of type BankAccount Create a new object of type BankAccount Call the second constructor (since a construction parameter is supplied) Call the second constructor (since a construction parameter is supplied) Set the parameter variable initialBalance to 1000 Set the parameter variable initialBalance to 1000 Set the balance instance field of the newly created object to initialBalance Set the balance instance field of the newly created object to initialBalance Return an object reference, that is, the memory location of the object, as the value of the new expression Return an object reference, that is, the memory location of the object, as the value of the new expression Store that object reference in the harrysChecking variable Store that object reference in the harrysChecking variable

33 Why put instructions in a method? … or why not just have one long list of instructions in our programs? 1.smaller  easier 2.test & debug once, execute as often as needed 3.more readable code

34 Implementing Methods Some methods do not return a value public void withdraw(double amount) { double newBalance = balance - amount; balance = newBalance; } Some methods return an output value public double getBalance() { return balance; }

35 Method Call Example harrysChecking.deposit(500); Set the parameter variable amount to 500 Set the parameter variable amount to 500 Fetch the balance field of the object whose location is stored in harrysChecking Fetch the balance field of the object whose location is stored in harrysChecking Add the value of amount to balance and store the result in the variable newBalance Add the value of amount to balance and store the result in the variable newBalance Store the value of newBalance in the balance instance field, overwriting the old value Store the value of newBalance in the balance instance field, overwriting the old value

36 Syntax 3.5: The return Statement return expression; or return;Example: return balance; Purpose: To specify the value that a method returns, and exit the method immediately. The return value becomes the value of the method call expression.

37

38

39

40 Wu’s Template

41 /** Stores information about one pet. */ public class Pet { /** Name of the pet. */ /** Name of the pet. */ private String name, kind; private int age; /** Initializes a new instance. * @param n The pet's name. * @param n The pet's name. */ */ public Pet( String n, String k, int a ) { public Pet( String n, String k, int a ) { name = n; kind = k; age = a; name = n; kind = k; age = a; } /** Returns the name of this pet. */ /** Returns the name of this pet. */ public String getName() { return name; } public String getName() { return name; } /** Changes the name of this pet. /** Changes the name of this pet. * @param newName The new name of the pet. * @param newName The new name of the pet. */ */ public void changeNameTo( String newName ) { public void changeNameTo( String newName ) { // TEST THE NAME HERE // TEST THE NAME HERE name = newName; name = newName; }} Data member Constructor Methods

42 3.6 Testing Classes Previous section was designing a class By itself, we cannot actually run a program No main() method, like most classes No main() method, like most classes We create instances of it in other classes Idea: use a test class to make sure it works properly before dispensing the product

43 3.6 Testing A Class Test class: a class with a main method that contains statements to test another class. Typically carries out the following steps: Construct one or more objects of the class that is being tested Construct one or more objects of the class that is being tested Invoke one or more methods Invoke one or more methods Print out one or more results Print out one or more results Verify output is correct and program behaves as expected Verify output is correct and program behaves as expected

44 /** A Test Program. */ public class TestPet { /** /** * Test Program starts here. * Test Program starts here. * @param args Unused in this program. * @param args Unused in this program. */ */ public static void main( String [] args ) { public static void main( String [] args ) { // Declare and create a pet // Declare and create a pet Pet pet1 = new Pet( "George", "bird", 14 ); Pet pet1 = new Pet( "George", "bird", 14 ); // Test the pet's information // Test the pet's information System.out.println( pet1.getName() + System.out.println( pet1.getName() + “ (should be George)” ); “ (should be George)” );}}

45 /** * A class to test the BankAccount class. * A class to test the BankAccount class. */ */ public class BankAccountTester{ /** /** *Tests the methods of the BankAccount class. *Tests the methods of the BankAccount class. *@param args not used *@param args not used */ */ public static void main(String[] args){ public static void main(String[] args){ BankAccount harrysChecking = new BankAccount(); BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit(2000); harrysChecking.deposit(2000); harrysChecking.withdraw(500); harrysChecking.withdraw(500); System.out.println(harrysChecking.getBalance()); System.out.println(harrysChecking.getBalance()); }}

46 3.7 Categories of Variables Categories of variables Instance fields (balance in BankAccount) Instance fields (balance in BankAccount) Local variables (newBalance in deposit method) Local variables (newBalance in deposit method) Parameter variables (amount in deposit method) Parameter variables (amount in deposit method) Share the same properties of declaring and creating Differ in their lifetime (AKA scope)

47 Instance Field An instance field belongs to an object AKA Instance variable AKA Instance variable Each object has its own copy of the instance field Each object has its own copy of the instance field The fields stay alive until no method uses the object any longer More specifically, until the object no longer exists

48 Local Variables Local and parameter variables belong to a method You declare them and create within the method You declare them and create within the method When method is done executing, variable is thrown out Every time the method is executed, a new copy of any variables is created, values do not carry over Every time the method is executed, a new copy of any variables is created, values do not carry over

49 Lifetime Of Variables Say we made the following method call harrysChecking.deposit(500); Remember that deposit looks like this public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; }

50

51 3.8 Implicit and Explicit Method Parameters The implicit parameter of a method is the object on which the method is invoked Why is this important? When a method is invoked, and an instance field is used, how does it know which object it belongs to?

52 Use of an instance field name in a method denotes the instance field of the implicit parameter – the object it is called on public void withdraw(double amount) { double newBalance = balance - amount; balance = newBalance; }

53 balance is the balance of the object to the left of the dot: momsSavings.withdraw(500);means double newBalance = momsSavings.balance - amount; momsSavings.balance = newBalance;

54 Implicit Parameters and this Every method has one implicit parameter The method knows what object called it based on a reference, but does not know/care about the identifier name E.g. you cannot say momsSavings.balance in the class, because momsSavings is a local variable of another class E.g. you cannot say momsSavings.balance in the class, because momsSavings is a local variable of another class So in order to be more general, we call the implicit parameter this Exception: Static methods do not have an implicit parameter (more on Chapter 9)

55 Implicit Parameters and this double newBalance = balance + amount; // actually means double newBalance = this.balance + amount; When you refer to an instance field in a method, the compiler automatically applies it to the this parameter

56 Example – without this public class BankAccount{ double balance; public BankAccount(double initialBalance){ balance = initialBalance; } public BankAccount(){ balance = 0; }…}

57 Example – with this public class BankAccount{ double balance; public BankAccount(double initialBalance){ balance = initialBalance; } public BankAccount(){ this(0);}…}

58 HOW TO 3.1 – Designing and Implementing a Class Step 1: Find out what you are asked to do with an object of the class What do you want to be able to do with the object? What do you want to be able to do with the object? Step 2: Specify the Public Interface Convert the list from step 1 into methods, and the parameters (think: input) they need Convert the list from step 1 into methods, and the parameters (think: input) they need Constructors: how do I want to initialize this object Constructors: how do I want to initialize this object

59 HOW-TO 3.1 (cont) Step 3: Document the public interface Create Javadoc comments for each method and the class Create Javadoc comments for each method and the class Step 4: Determine instance fields What information needs to be maintained? What information needs to be maintained? Step 5: Implement One at a time, from easiest to most difficult One at a time, from easiest to most difficult

60 HOW-TO 3.1 (cont) Go back to Step 2 if implementation doesn’t work Step 6: Test your class

61 Note on Style #1 A couple ways to use curly braces A couple ways to use curly braces Option #1 public class SomeClass{ …} Option #2 public class SomeClass {…}

62 Note on Style #2 Book says to place instance fields at the bottom of a class public class SomeClass{ //constructors//methods //instance fields } Most conventions have it at the top of a class public class SomeClass{ //instance fields //constructors//methods}


Download ppt "Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To."

Similar presentations


Ads by Google