Class Design Examples
Problem Solving <ul><li>Solving a problem means that we know the way or the method to follow manually from the start till ...
Problem Solving Strategy
The Class Design Steps <ul><li>Before starting any coding, good programmers should always design the classes. </li></ul><u...
Example 1: Designing a Car Class <ul><li>A class  “Car”  that calculates miles per gallon.   </li></ul><ul><li>What data i...
Class Definition: Step by Step   <ul><li>import java.io.* ; </li></ul><ul><li>class Car  </li></ul><ul><li>{  </li></ul><u...
Class Definition: Step by Step <ul><li>import java.io.* ; </li></ul><ul><li>class Car  </li></ul><ul><li>{  </li></ul><ul>...
Class Definition: Step by Step <ul><li>import java.io.* ; </li></ul><ul><li>class Car  </li></ul><ul><li>{  </li></ul><ul>...
Complete Program <ul><li>import java.io.* ; </li></ul><ul><li>class Car  </li></ul><ul><li>{  </li></ul><ul><li>// instanc...
Complete Program (cont .....) <ul><li>class MilesPerGallon  </li></ul><ul><li>{  </li></ul><ul><li>public static void main...
Two Car Objects <ul><li>Can several objects of type Car be constructed ?   Yes </li></ul><ul><li>import java.io.* ; </li><...
Example 2: Designing a class “CheckingAccount” <ul><li>To design “CheckingAccount” class, we follow the same steps which w...
Example 2: Designing a class “CheckingAccount” <ul><li>class CheckingAccount  </li></ul><ul><li>{  </li></ul><ul><li>// in...
Class Definition <ul><li>class CheckingAccount </li></ul><ul><li>{  </li></ul><ul><li>// instance variables   </li></ul><u...
CheckingAccount: More Requirements Analysis <ul><li>The requirements also describe what each method does.  </li></ul><ul><...
Complete Class <ul><li>class CheckingAccount {  </li></ul><ul><li>// instance variables   </li></ul><ul><li>.......  </li>...
Using a CheckingAccount Object <ul><li>class CheckingAccount {  </li></ul><ul><li>. . . . ......... </li></ul><ul><li>}  <...
Encapsulation and Visibility Modifiers
What is “Encapsulation” <ul><li>So far, the objects we have designed, have all of their members (variables and methods) vi...
Why is “Encapsulation” <ul><li>Encapsulation provides security to object data by specifying who is allowed to access one o...
The “private” visibility modifier <ul><li>When a member (a variable or method) of a class is declared  private  it can be ...
main can’t access private members <ul><li>class CheckingAccountTester { </li></ul><ul><li>public static void main( String[...
Access Methods <ul><li>A class with private members, controls access to those members by using  access methods .  </li></u...
CheckingAccount Class with Access Methods <ul><li>class CheckingAccount {  </li></ul><ul><li>// instance variables  </li><...
main uses access methods to access private members <ul><li>class CheckingAccountTester {  </li></ul><ul><li>public static ...
Private Methods <ul><li>Can we have a private method in a class?  Yes </li></ul><ul><li>private method  can be used only b...
Private Methods Example: incrementUse( ) <ul><li>To do this, we add the following members to CheckingAccount class:   </li...
CheckingAccount class with incrementUse( ) <ul><li>class CheckingAccount {  </li></ul><ul><li>private String accountNumber...
main can’t use private method <ul><li>The main() cannot use the private variable useCount nor can it use the private metho...
display( ) Method <ul><li>As main() cannot access the private variables, so it is good idea to have a display() method to ...
The public Visibility Modifier <ul><li>The  public  access modifier explicitly says that a method or variable of an object...
The public Visibility Modifier <ul><li>class CheckingAccount {  </li></ul><ul><li>private String accountNumber;  </li></ul...
Default Visibility <ul><li>If you do not specify public or private for a variable or a method, then it will have default v...
Parameters, Local Variables, Overloading
Parameters and Local Variables <ul><li>An object stores its  state  as values in its instance variables. </li></ul><ul><li...
<ul><li>A  parameter  is a  variable name  used in a method definition. </li></ul><ul><li>class CheckingAccount { </li></u...
What is the use of a Parameter <ul><li>It is used to store the values passed to the method by its callers. </li></ul><ul><...
Formal and Actual Parameters <ul><li>Formal parameter —the identifier used in a method definition in which the value is pa...
Scope of a Parameter <ul><li>The scope of a parameter is the body of its method.   </li></ul><ul><li>class CheckingAccount...
Local Variables <ul><li>A  local variable  is a variable that is declared inside of the body of a method.  </li></ul><ul><...
Method Overloading <ul><li>Overloading  is when two or more methods of a class have  the same name but have different para...
Method Overloading class CheckingAccount { private int balance;   . . . .  void processDeposit( int amount )  { balance = ...
Method Signature <ul><li>The  signature  of a method is:  </li></ul><ul><ul><li>Its name.  </li></ul></ul><ul><ul><li>The ...
Call by Value & Primitive Parameters <ul><li>Java uses  call by value  for parameters. </li></ul><ul><li>Changes to the pr...
Object Parameters <ul><li>An object can also be passed as a parameter to a method. </li></ul><ul><li>Again call by value i...
<ul><li>class MyPoint {  </li></ul><ul><li>public int x=3, y=5 ;  </li></ul><ul><li>public void print() {  </li></ul><ul><...
<ul><li>class ObjectPrinter {  </li></ul><ul><li>public void print( String st ) {  </li></ul><ul><li>st = &quot;Hah! A sec...
Upcoming SlideShare
Loading in...5
×

Java: Class Design Examples

8,807

Published on

Sub: Java
Topic: Class Design Examples
Slide number: 3
Presented by: Mahbubul Islam (MMI)
Lecturer, Dept. of CSE
University of Rajshahi

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,807
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
167
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Java: Class Design Examples

  1. 1. Class Design Examples
  2. 2. Problem Solving <ul><li>Solving a problem means that we know the way or the method to follow manually from the start till the end. </li></ul><ul><li>Having the method known, the same method is used by the computer to solve the problem but faster with higher precision. </li></ul><ul><li>If we do not know how to solve a problem ourselves, the computer will not be of any help in this regard. </li></ul><ul><li>The strategy for solving a problem goes through the following stages: </li></ul><ul><ul><li>Analysis: in this stage, we should find what the problem should do. </li></ul></ul><ul><ul><li>Design : the way or method of how your problem is solved is produced </li></ul></ul><ul><ul><li>Implementation: the method found in design is then coded here in a given programming language. </li></ul></ul><ul><ul><li>Testing: here we verify that the program written is working correctly </li></ul></ul><ul><ul><li>Deployment : finally the program is ready to use </li></ul></ul>
  3. 3. Problem Solving Strategy
  4. 4. The Class Design Steps <ul><li>Before starting any coding, good programmers should always design the classes. </li></ul><ul><li>A class design consists of deciding following </li></ul><ul><ul><li>What data they will contain </li></ul></ul><ul><ul><li>How they will behave </li></ul></ul><ul><li>After class design and coding, its objects are used. </li></ul><ul><li>We’ll learn all the above steps by designing a class Car and a class CheckingAccount . </li></ul>
  5. 5. Example 1: Designing a Car Class <ul><li>A class “Car” that calculates miles per gallon. </li></ul><ul><li>What data it will contain ? Variables </li></ul><ul><ul><li>int startMiles;      // Starting odometer reading </li></ul></ul><ul><ul><li>int endMiles;       // Ending odometer reading </li></ul></ul><ul><ul><li>double gallons;    // Gallons of gas used between the readings </li></ul></ul><ul><li>How it will behave ? </li></ul><ul><li>Constructors Car ( int startOdo, int endingOdo, double gallons ) </li></ul><ul><ul><ul><li>Creates a new instance of a Car object with the starting and ending odometer readings and the number of gallons of gas consumed. </li></ul></ul></ul><ul><li>Methods </li></ul><ul><ul><li>double calculateMPG() </li></ul></ul><ul><ul><ul><li>calculates and returns the miles per gallon for the car. </li></ul></ul></ul>
  6. 6. Class Definition: Step by Step <ul><li>import java.io.* ; </li></ul><ul><li>class Car </li></ul><ul><li>{ </li></ul><ul><li>// instance variables </li></ul><ul><li>// constructor </li></ul><ul><li>// methods </li></ul><ul><li>} </li></ul><ul><li>class MilesPerGallon </li></ul><ul><li>{ </li></ul><ul><li>.............. </li></ul><ul><li>} </li></ul>
  7. 7. Class Definition: Step by Step <ul><li>import java.io.* ; </li></ul><ul><li>class Car </li></ul><ul><li>{ </li></ul><ul><li>// instance variables </li></ul><ul><li>int startMiles; // Stating odometer reading </li></ul><ul><li>int endMiles; // Ending odometer reading </li></ul><ul><li>double gallons; // Gallons of gas used between the readings </li></ul><ul><li>// constructor </li></ul><ul><li>// methods </li></ul><ul><li>} </li></ul><ul><li>class MilesPerGallon </li></ul><ul><li>{ </li></ul><ul><li>.................... </li></ul><ul><li>} </li></ul>
  8. 8. Class Definition: Step by Step <ul><li>import java.io.* ; </li></ul><ul><li>class Car </li></ul><ul><li>{ </li></ul><ul><li>// instance variables </li></ul><ul><li>int startMiles; // Stating odometer reading </li></ul><ul><li>int endMiles; // Ending odometer reading </li></ul><ul><li>double gallons; // Gallons of gas used between the readings </li></ul><ul><li>// constructor </li></ul><ul><li>Car( int first, int last, double gals ) </li></ul><ul><li>{ </li></ul><ul><li>startMiles = first; endMiles = last; gallons = gals; </li></ul><ul><li>} </li></ul><ul><li>// methods </li></ul><ul><li>} </li></ul><ul><li>class MilesPerGallon </li></ul><ul><li>{ </li></ul><ul><li>.................... </li></ul><ul><li>} </li></ul>
  9. 9. Complete Program <ul><li>import java.io.* ; </li></ul><ul><li>class Car </li></ul><ul><li>{ </li></ul><ul><li>// instance variables </li></ul><ul><li>int startMiles; // Stating odometer reading </li></ul><ul><li>int endMiles; // Ending odometer reading </li></ul><ul><li>double gallons; // Gallons of gas used between the readings </li></ul><ul><li>// constructor </li></ul><ul><li>Car( int first, int last, double gals ) </li></ul><ul><li>{ </li></ul><ul><li>startMiles = first; endMiles = last; gallons = gals; </li></ul><ul><li>} </li></ul><ul><li>// methods </li></ul><ul><li>double calculateMPG( ) </li></ul><ul><li>{ </li></ul><ul><li>return (endMiles - startMiles)/gallons ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  10. 10. Complete Program (cont .....) <ul><li>class MilesPerGallon </li></ul><ul><li>{ </li></ul><ul><li>public static void main( String[] args ) </li></ul><ul><li>{ </li></ul><ul><li>Car car = new Car( 32456, 32810, 10.6 ); </li></ul><ul><li>System.out.println( &quot;Miles per gallon is &quot; + car.calculateMPG() ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  11. 11. Two Car Objects <ul><li>Can several objects of type Car be constructed ? Yes </li></ul><ul><li>import java.io.* ; </li></ul><ul><li>class Car </li></ul><ul><li>{ </li></ul><ul><li>....................... </li></ul><ul><li>} </li></ul><ul><li>class MilesPerGallon </li></ul><ul><li>{ </li></ul><ul><li>public static void main( String[] args ) </li></ul><ul><li>{ </li></ul><ul><li>Car car1 = new Car( 32456, 32810, 10.6 ); </li></ul><ul><li>System.out.println( &quot;Miles per gallon of car1 is &quot; + car1.calculateMPG() ); </li></ul><ul><li>Car car2 = new Car( 100000, 100300, 10.6 ); </li></ul><ul><li>System.out.println( &quot;Miles per gallon of car 2 is &quot; + car2.calculateMPG() ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  12. 12. Example 2: Designing a class “CheckingAccount” <ul><li>To design “CheckingAccount” class, we follow the same steps which we followed for class Car. </li></ul><ul><ul><li>Requirement analysis: What data it will contain </li></ul></ul><ul><ul><ul><li>Account number. </li></ul></ul></ul><ul><ul><ul><li>Name of account holder. </li></ul></ul></ul><ul><ul><ul><li>Current balance. </li></ul></ul></ul><ul><ul><li>Design: How it will behave (what actions will be performed on a checking account) </li></ul></ul><ul><ul><ul><li>Accept a deposit. </li></ul></ul></ul><ul><ul><ul><li>Process a check. </li></ul></ul></ul><ul><ul><ul><li>Get the current balance. </li></ul></ul></ul><ul><li>Also, we need a constructor to create objects from the class. </li></ul>
  13. 13. Example 2: Designing a class “CheckingAccount” <ul><li>class CheckingAccount </li></ul><ul><li>{ </li></ul><ul><li>// instance variables </li></ul><ul><li>String accountNumber; </li></ul><ul><li>String accountHolder; </li></ul><ul><li>int balance; </li></ul><ul><li>// constructor </li></ul><ul><li>// methods </li></ul><ul><li>} </li></ul>
  14. 14. Class Definition <ul><li>class CheckingAccount </li></ul><ul><li>{ </li></ul><ul><li>// instance variables </li></ul><ul><li>String accountNumber; </li></ul><ul><li>String accountHolder; </li></ul><ul><li>int balance; </li></ul><ul><li>//constructor </li></ul><ul><li>CheckingAccount( String accNumber, String holder, int start ) </li></ul><ul><li>{ </li></ul><ul><li>accountNumber = accNumber ; </li></ul><ul><li>accountHolder = holder ; </li></ul><ul><li>balance = start ; </li></ul><ul><li>} </li></ul><ul><li>// methods </li></ul><ul><li>} </li></ul>
  15. 15. CheckingAccount: More Requirements Analysis <ul><li>The requirements also describe what each method does. </li></ul><ul><li>For example, </li></ul><ul><ul><li>The method Accept a deposit adds an amount to the current balance. </li></ul></ul><ul><ul><li>The method Process a check subtracts a given amount from the current balance. </li></ul></ul><ul><ul><li>The method Get the current balance returns the current balance in checking account </li></ul></ul><ul><li>Following the above requirements, now we can write method definitions. </li></ul>
  16. 16. Complete Class <ul><li>class CheckingAccount { </li></ul><ul><li>// instance variables </li></ul><ul><li>....... </li></ul><ul><li>//constructor </li></ul><ul><li>................ </li></ul><ul><li>// methods </li></ul><ul><li>int currentBalance() { </li></ul><ul><li>return balance ; </li></ul><ul><li>} </li></ul><ul><li>void processDeposit( int amount ) { </li></ul><ul><li>balance = balance + amount ; </li></ul><ul><li>} </li></ul><ul><li>void processCheck( int amount ) { </li></ul><ul><li>balance = balance – amount </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  17. 17. Using a CheckingAccount Object <ul><li>class CheckingAccount { </li></ul><ul><li>. . . . ......... </li></ul><ul><li>} </li></ul><ul><li>class CheckingAccountTester { </li></ul><ul><li>public static void main( String[] args ) { </li></ul><ul><li>CheckingAccount account1 = new CheckingAccount( &quot;123&quot;, &quot;Bob&quot;, 100 ); </li></ul><ul><li>System.out.println( account1.currentBalance() ); account1.processDeposit( 2000 ); </li></ul><ul><li>account1.processCheck( 1500 ); </li></ul><ul><li>System.out.println( account1.currentBalance() ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  18. 18. Encapsulation and Visibility Modifiers
  19. 19. What is “Encapsulation” <ul><li>So far, the objects we have designed, have all of their members (variables and methods) visible to any part of the program. </li></ul><ul><li>Thus, code outside of an object can access (see and change) the object's members. </li></ul><ul><li>A better OOP design enforces encapsulation: </li></ul><ul><ul><li>Some (or all) of an object's member variables and methods should be visible only to the object's own methods. </li></ul></ul><ul><ul><li>Thus, code outside of the object should not be allowed to access these members directly. </li></ul></ul>
  20. 20. Why is “Encapsulation” <ul><li>Encapsulation provides security to object data by specifying who is allowed to access one of its variables or methods. </li></ul><ul><li>The encapsulation can be achieved by using visibility modifiers. </li></ul>
  21. 21. The “private” visibility modifier <ul><li>When a member (a variable or method) of a class is declared private it can be used only by the methods of that class. </li></ul><ul><li>class CheckingAccount </li></ul><ul><li>{ </li></ul><ul><li>// instance variables </li></ul><ul><li> private String accountNumber; </li></ul><ul><li> private String accountHolder; </li></ul><ul><li> private int balance; </li></ul><ul><li>.................. </li></ul><ul><li>} </li></ul><ul><li>Now only the methods of a CheckingAccount object can access the values in accountNumber, accountHolder, and balance. </li></ul>
  22. 22. main can’t access private members <ul><li>class CheckingAccountTester { </li></ul><ul><li>public static void main( String[] args ) { </li></ul><ul><li>CheckingAccount bobsAccount = new CheckingAccount( &quot;999&quot;, &quot;Bob&quot;, 100 ); </li></ul><ul><li>System.out.println(bobsAccount.balance); </li></ul><ul><li>bobsAccount.balance = bobsAccount.balance + 200; </li></ul><ul><li>System.out.println( bobsAccount.balance ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>What happens when we try to compile the above program ? </li></ul><ul><li>We get a Compilation Errors because balance is private, and therefore main( ) is not allowed to access its value. </li></ul><ul><li>How can main( ) access the balance of a CheckingAccount object ? </li></ul><ul><li> By using Access Method </li></ul>
  23. 23. Access Methods <ul><li>A class with private members, controls access to those members by using access methods . </li></ul><ul><li>An access method is a method which </li></ul><ul><ul><li>is used to access the private members of the class </li></ul></ul><ul><ul><li>is visible to other classes (usually public). </li></ul></ul><ul><li>The outside code can access private members of an object only by using access methods. </li></ul><ul><li>For example, main() can access balance of a CheckingAccount object by using the access methods currentBalance() </li></ul>
  24. 24. CheckingAccount Class with Access Methods <ul><li>class CheckingAccount { </li></ul><ul><li>// instance variables </li></ul><ul><li>....... </li></ul><ul><li>//constructor </li></ul><ul><li>................ </li></ul><ul><li>// Access methods </li></ul><ul><li>int currentBalance() { </li></ul><ul><li>return balance ; </li></ul><ul><li>} </li></ul><ul><li>void processDeposit( int amount ) { </li></ul><ul><li>balance = balance + amount ; </li></ul><ul><li>} </li></ul><ul><li>void processCheck( int amount ) { </li></ul><ul><li>balance = balance – amount; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  25. 25. main uses access methods to access private members <ul><li>class CheckingAccountTester { </li></ul><ul><li>public static void main( String[] args ) { </li></ul><ul><li>CheckingAccount account1 = new CheckingAccount( &quot;123&quot;, &quot;Bob&quot;, 100 ); </li></ul><ul><li>System.out.println( account1.currentBalance()); </li></ul><ul><li>account1.processDeposit( 2000 ); </li></ul><ul><li>account1.processCheck( 1500 ); </li></ul><ul><li>System.out.println(account1.currentBalance()); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount bobsAccount = new CheckingAccount(&quot;999&quot;,&quot;Bob&quot;, 100); System.out.println(bobsAccount.balance); bobsAccount.balance = bobsAccount.balance + 2000; bobsAccount.balance = bobsAccount.balance -1500; System.out.println( bobsAccount.balance ); } }
  26. 26. Private Methods <ul><li>Can we have a private method in a class? Yes </li></ul><ul><li>private method can be used only by the other methods of the object. Program code outside the object cannot directly use a private method of the object. </li></ul><ul><li>Say that the bank wants to keep track of how many times each checking account is used. </li></ul><ul><li>A &quot;use&quot; of the checking account is processing a deposit processDeposit() or processing a check processCheck() . </li></ul>
  27. 27. Private Methods Example: incrementUse( ) <ul><li>To do this, we add the following members to CheckingAccount class: </li></ul><ul><ul><li>A private variable &quot;useCount“ </li></ul></ul><ul><ul><li>A private method “incrementUse” </li></ul></ul><ul><li>The incrementUse() is used by processDeposit() and processCheck() methods each time they are used, to increase the value of useCount by (1). </li></ul>
  28. 28. CheckingAccount class with incrementUse( ) <ul><li>class CheckingAccount { </li></ul><ul><li>private String accountNumber; </li></ul><ul><li>private String accountHolder; </li></ul><ul><li>private int balance; </li></ul><ul><li>private int useCount = 0; </li></ul><ul><li>private void incrementUse( ) { </li></ul><ul><li>useCount = useCount + 1; </li></ul><ul><li>} </li></ul><ul><li>void processDeposit( int amount ) { </li></ul><ul><li>balance = balance + amount ; </li></ul><ul><li>incrementUse( ); </li></ul><ul><li>} </li></ul><ul><li>void processCheck( int amount ) { </li></ul><ul><li>balance = balance – amount; </li></ul><ul><li>incrementUse( ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  29. 29. main can’t use private method <ul><li>The main() cannot use the private variable useCount nor can it use the private method incrementUse(). </li></ul><ul><li>The main() can use bobsAccount.processCheck() which is not private. It in turn uses the private method incrementUse(). </li></ul><ul><li>class CheckingAccountTester { </li></ul><ul><li>public static void main( String[ ] args ) { </li></ul><ul><li>CheckingAccount bobsAccount = new CheckingAccount( &quot;999&quot;, &quot;Bob&quot;, 100 ); </li></ul><ul><li>bobsAccount.processCheck( 50 ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  30. 30. display( ) Method <ul><li>As main() cannot access the private variables, so it is good idea to have a display() method to print the private variables. </li></ul><ul><li>class CheckingAccount { </li></ul><ul><li>private String accountNumber; </li></ul><ul><li>private String accountHolder; </li></ul><ul><li>private int balance; </li></ul><ul><li>private int useCount = 0; </li></ul><ul><li>. . . . . . . . . . </li></ul><ul><li>void display( ) { </li></ul><ul><li>System.out.println( accountNumber + &quot; &quot; + accountHolder + &quot; &quot; + balance &quot; &quot; + useCount ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  31. 31. The public Visibility Modifier <ul><li>The public access modifier explicitly says that a method or variable of an object can be accessed by code outside of the object. </li></ul><ul><li>The access methods are intended for outsiders, and have to be visible to outsiders in order to be useful. </li></ul><ul><li>So, the public visibility modifier is usually used for all access methods and constructors in a class definition. </li></ul>
  32. 32. The public Visibility Modifier <ul><li>class CheckingAccount { </li></ul><ul><li>private String accountNumber; </li></ul><ul><li>private String accountHolder; </li></ul><ul><li>private int balance; </li></ul><ul><li>private int useCount = 0; </li></ul><ul><li>private void incrementUse( ) { .........} </li></ul><ul><li>public CheckingAccount( String accNumber, String holder, int start ) { . . . . } </li></ul><ul><li>public void processDeposit( int amount ) { . . . . } </li></ul><ul><li>public void processCheck( int amount ) { . . . . } </li></ul><ul><li>public int currentBalance( ) { . . . . } </li></ul><ul><li>public void display( ) { . . . . } </li></ul><ul><li>} </li></ul>
  33. 33. Default Visibility <ul><li>If you do not specify public or private for a variable or a method, then it will have default visibility. </li></ul><ul><li>Default visibility allows a variable or method to be seen within all methods of a class or other classes that are part of the same package . </li></ul><ul><li>For now, default visibility means about the same thing as public visibility. But it is best to explicitly declare members public if that is what you actually need. </li></ul><ul><li>You can not explicitly declare a member of a class to have default visibility (for example, you can't say default int monthlyCharge; ) </li></ul>
  34. 34. Parameters, Local Variables, Overloading
  35. 35. Parameters and Local Variables <ul><li>An object stores its state as values in its instance variables. </li></ul><ul><li>But sometimes an object's methods work with values that are not part of an object's state. </li></ul><ul><li>These values are stored in the parameters and local variables . </li></ul>
  36. 36. <ul><li>A parameter is a variable name used in a method definition. </li></ul><ul><li>class CheckingAccount { </li></ul><ul><li>. . . . </li></ul><ul><li>private int balance; </li></ul><ul><li>. . . . </li></ul><ul><li>void processDeposit(int amount ) { </li></ul><ul><li>balance = balance + amount ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>What is a Parameter
  37. 37. What is the use of a Parameter <ul><li>It is used to store the values passed to the method by its callers. </li></ul><ul><li>In this example, the value 200 passed by main() is stored in the parameter “ amount” </li></ul>class CheckingAccountTester { public static void main(String[] args) { CheckingAccount bobsAccount= new CheckingAccount(&quot;999&quot;, &quot;Bob&quot;, 100 ); bobsAccount.processDeposit( 200 ); } }
  38. 38. Formal and Actual Parameters <ul><li>Formal parameter —the identifier used in a method definition in which the value is passed. </li></ul><ul><ul><li>amount is a formal parameter of processDeposit </li></ul></ul><ul><li>Actual parameter —the actual value that is passed into the method by a caller. </li></ul><ul><ul><li>200 is an actual parameter of processDeposit . </li></ul></ul>
  39. 39. Scope of a Parameter <ul><li>The scope of a parameter is the body of its method. </li></ul><ul><li>class CheckingAccount { </li></ul><ul><li>void processDeposit(int amount ) { </li></ul><ul><li>// scope of amount starts here </li></ul><ul><li> balance = balance + amount ; </li></ul><ul><li>// scope of amount ends here </li></ul><ul><li>} </li></ul><ul><li>void display() { </li></ul><ul><li>System.out.println(balance+&quot; &quot;+ amount ; //syntax error </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>The display() method is outside the scope of amount . </li></ul>
  40. 40. Local Variables <ul><li>A local variable is a variable that is declared inside of the body of a method. </li></ul><ul><li>It is used to hold a temporary value. </li></ul>class CheckingAccount { private int balance; void processCheck(int amount) { int charge; // scope of charge starts here ........................ // scope of charge ends here } }
  41. 41. Method Overloading <ul><li>Overloading is when two or more methods of a class have the same name but have different parameter lists . </li></ul><ul><li>When one of the methods is called, it is made clear which one is wanted by matching the actual parameter list to the formal parameter lists . </li></ul>
  42. 42. Method Overloading class CheckingAccount { private int balance; . . . . void processDeposit( int amount ) { balance = balance + amount ; } void processDeposit( int amount, int serviceCharge ) { balance = balance + amount - serviceCharge; } } class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount bobsAccount = new CheckingAccount( &quot;999&quot;, &quot;Bob&quot;, 100 ); bobsAccount.processDeposit( 200 ); // call to first bobsAccount.processDeposit( 200, 25 ); // call to second } }
  43. 43. Method Signature <ul><li>The signature of a method is: </li></ul><ul><ul><li>Its name. </li></ul></ul><ul><ul><li>The number and types of its parameters, in order. </li></ul></ul><ul><li>For example, the signatures of the two processDeposit methods are: </li></ul><ul><ul><li>processDeposit( int ) </li></ul></ul><ul><ul><li>processDeposit( int, int ) </li></ul></ul><ul><li>The signatures of the methods in a class must be unique. </li></ul><ul><li>The return type is not part of the signature </li></ul>
  44. 44. Call by Value & Primitive Parameters <ul><li>Java uses call by value for parameters. </li></ul><ul><li>Changes to the primitive parameter do not affect the Caller </li></ul>class SimpleClass { public void work( int x ) { x = 100; // local change to the formal parameter } } class SimpleTester { public static void main ( String[] args ) { int var = 7; SimpleClass simple = new SimpleClass(); System.out.println(&quot;First value of the local var: &quot; + var ); simple.work( var ); System.out.println(&quot;Second value of the local var: &quot; + var ); } } Output First value of the local var: 7 Second value of the local var: 7
  45. 45. Object Parameters <ul><li>An object can also be passed as a parameter to a method. </li></ul><ul><li>Again call by value is used, but now the value is a reference to an object. </li></ul><ul><li>Using a object reference parameter, the contents of a mutable object can be changed, and the change will affect the caller. </li></ul><ul><li>However, Immutable objects (like String objects) can‘t be changed </li></ul>
  46. 46. <ul><li>class MyPoint { </li></ul><ul><li>public int x=3, y=5 ; </li></ul><ul><li>public void print() { </li></ul><ul><li>System.out.println(&quot;x = &quot; + x + &quot;; y = &quot; + y ); </li></ul><ul><li>} } </li></ul><ul><li>class PointDoubler { </li></ul><ul><li>public void twice( MyPoint parm ) { </li></ul><ul><li>parm.x = parm.x * 2 ; </li></ul><ul><li>parm.y = parm.y * 2 ; </li></ul><ul><li>} } </li></ul><ul><li>class PointTester { </li></ul><ul><li>public static void main ( String[ ] args ) { </li></ul><ul><li>MyPoint pt = new MyPoint( ); </li></ul><ul><li>PointDoubler dbl = new PointDoubler( ); </li></ul><ul><li>pt.print( ); </li></ul><ul><li>dbl.twice( pt ); </li></ul><ul><li>pt.print(); </li></ul><ul><li>} } </li></ul>Mutable Object Parameters: MyPoint x = 3; y = 5 x = 6; y = 10
  47. 47. <ul><li>class ObjectPrinter { </li></ul><ul><li>public void print( String st ) { </li></ul><ul><li>st = &quot;Hah! A second Object!&quot; ; </li></ul><ul><li>}} </li></ul><ul><li>class OPTester2 { </li></ul><ul><li>public static void main ( String[] args ) { </li></ul><ul><li>String message = “Welcome&quot; ; </li></ul><ul><li>ObjectPrinter op = new ObjectPrinter( ); </li></ul><ul><li>System.out.println(&quot;First value of message: &quot; + message ); </li></ul><ul><li> op.print( message ); </li></ul><ul><li>System.out.println(&quot;Second value of message: &quot; + message ); </li></ul><ul><li>} } </li></ul>Immutable Object Parameters: String First value of message: Welcome Second value of message: Welcome
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×