Chapter 6. Overloading Methods and Constructors  Two or more methods in a class may have the same name as long as their parameter lists are different.

Slides:



Advertisements
Similar presentations
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Advertisements

Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Chapter 6: A First Look at Classes
Writing Classes (Chapter 4)
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Introduction to Object-Oriented Programming
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Starting Out with Java: From Control Structures through Objects.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Starting Out with Java: From Control Structures through Objects
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Dialog Boxes.
ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Programming Languages and Paradigms Activation Records in Java.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
C# Programming Methods.
Classes - Intermediate
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 5.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Chapter 3 Implementing Classes
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Import javax.swing.JOptionPane; public class Rectangle { public static void main(String[] args) { double width, length, area, perimeter; String lengthStr,
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Lecture 3 John Woodward.
Chapter 3 Classes & Objects
Implementing Classes Yonglei Tao.
Dialogues and Wrapper Classes
suggested reading: Java Ch. 6
More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.
Chapter 6: A First Look at Classes
Chapter Three - Implementing Classes
You can work in groups on this program.
Chapter 3 Implementing Classes
د.سناء الصايغ الفصل الأول البرمجة الشيئية
More on Classes and Objects
Unit-2 Objects and Classes
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Introduction to Object-Oriented Programming
Starting Out with Java: From Control Structures through Objects
Presentation transcript:

Chapter 6

Overloading Methods and Constructors  Two or more methods in a class may have the same name as long as their parameter lists are different.  When this occurs, it is called method overloading. This also applies to constructors.  Method overloading is important because sometimes you need several different ways to perform the same operation.

Overloaded Method add public int add(int num1, int num2) { int sum = num1 + num2; return sum; } public String add (String str1, String str2) { String combined = str1 + str2; return combined; }

Method Signature and Binding  A method signature consists of the method’s name and the data types of the method’s parameters, in the order that they appear. The return type is not part of the signature. add(int, int) add(String, String)  The process of matching a method call with the correct method is known as binding. The compilier uses the method signature to determine which version of the overloaded method to bind the call to. Signatures of the add methods of previous slide

Rectangle Class Constructor Overload If we were to add the no-args constructor we wrote previously to our Rectangle class in addition to the original constructor we wrote, what would happen when we execute the following calls? Rectangle box1 = new Rectangle(); Rectangle box2 = new Rectangle(5.0, 10.0);

Rectangle Class Constructor Overload If we were to add the no-args constructor we wrote previously to our Rectangle class in addition to the original constructor we wrote, what would happen when we execute the following calls? Rectangle box1 = new Rectangle(); Rectangle box2 = new Rectangle(5.0, 10.0); The first call would use the no-args constructor and box1 would have a length of 1.0 and width of 1.0. The second call would use the original constructor and box2 would have a length of 5.0 and a width of 10.0.

The BankAccount Example BankAccount -balance:double +BankAccount(): +BankAccount(startBalance:double): +BankAccount(strString): +deposit(amount:double):void +deposit(str:String):void +withdraw(amount:double):void +withdraw(str:String):void +setBalance(b:double):void +setBalance(str:String):void +getBalance():double Overloaded Constructors Overloaded deposit methods Overloaded withdraw methods Overloaded setBalance methods

BankAccount.java public class BankAccount { private double balance; // Account balance public BankAccount() { balance = 0.0; } public BankAccount(double startBalance) { balance = startBalance; } public BankAccount(String str) { balance = Double.parseDouble(str); } public void deposit(double amount) { balance += amount; } public void deposit(String str) { balance += Double.parseDouble(str); }

BankAccount.java  public void withdraw(double amount) {  balance -= amount;  }  public void withdraw(String str) {  balance -= Double.parseDouble(str);  }  public void setBalance(double b) {  balance = b;  }  public void setBalance(String str) {  balance = Double.parseDouble(str);  }  public double getBalance() {  return balance;  }

AccountTest.java import javax.swing.JOptionPane; // For the JOptionPane class import java.text.DecimalFormat; // For the DecimalFormat class public class AccountTest { public static void main(String[] args) { String input; // To hold user input DecimalFormat dollar = new DecimalFormat("#,###.00"); input = JOptionPane.showInputDialog("What is your " + "account's starting balance?"); BankAccount account = new BankAccount(input); input = JOptionPane.showInputDialog("How much were " + "you paid this month? "); account.deposit(input);

AccountTest.java JOptionPane.showMessageDialog(null, "Your pay has been deposited.\n" + "Your current balance is $ " + dollar.format(account.getBalance())); input = JOptionPane.showInputDialog("How much would " + "you like to withdraw? "); account.withdraw(input); JOptionPane.showMessageDialog(null, "Now your balance is $" + dollar.format(account.getBalance())); System.exit(0); }