©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 1 Chapter 7 More on Defining Classes Overloading.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Introduction to OOP with Java 4th Ed, C. Thomas Wu
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting with Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 7- 1 Chapter 7 Defining Your Own Classes Part 2.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
Programmer-defined classes Part 3. A class for representing Fractions public class Fraction { private int numerator; private int denominator; public Fraction(int.
Chapter Four Defining Your Own Classes continued.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Image Slides.
Chapter 8 Traffic-Analysis Techniques. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Why Programmer-Defined Classes Using just the String,
Defining Your Own Classes Part 3. class { } Template for Class Definition Import Statements Class Comment Class Name Data Members Methods (incl. Constructor)
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Ahmad.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
ECE122 Feb. 22, Any question on Vehicle sample code?
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 Defining Your Own Classes Part 2 Animated Version.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2 (adapted.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
DCS 2133 Object Oriented Programming Defining Your Own Classes Part 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 Defining Your Own Classes Part 2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
1 Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality.
Object Oriented Programming COP3330 / CGS5409.  Classes & Objects  DDU Design  Constructors  Member Functions & Data  Friends and member functions.
Chapter 13 Transportation Demand Analysis. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
Chapter 4 : Defining Your Own Classes Part 1 - Objectives After you have read and studied this chapter, you should be able to Define a class with multiple.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 3: Using Methods, Classes, and Objects
Defining Your Own Classes
Variables and Their scope
Introduction to Object-Oriented Programming with Java--Wu
More on Classes and Objects
Class rational part2.
Chapter 11 Classes.
Chapter 7 Defining Your Own Classes Part 2
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading methods and constructors Parameter Passing Objects as parameters this reference Class methods (static)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Overloaded Methods Methods can share the same name as long as –they have a different number of parameters (Rule 1) or –their parameters are of different data types when the number of parameters is the same (Rule 2) public void myMethod(int x, int y) {... } public void myMethod(int x) {... } public void myMethod(double x) {... } public void myMethod(int x) {... } Rule 1 Rule 2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Overloaded Constructors The same rules apply for overloaded constructors –this is how we can define more than one constructor to a class –The Student class has two constructors public Person( ) {... } public Person(int age) {... } public Pet(int age) {... } public Pet(String name) {... } Rule 1 Rule 2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Call-by-Value Parameter Passing When a method is called, –memory is allocated for each parameter variable –the value of the argument is copied into the matching parameter variable This way of passing the value of arguments is called a pass-by-value or call-by-value scheme. –the parameter is local to the method –changes to the parameter do not affect the value of the corresponding argument.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Call-by-Value Example class Tester { public void myMethod(int one, double two ) { one = 25; two = 35.4; } Tester tester; int x, y; tester = new Tester(); x = 10; y = 20; tester.myMethod(x, y); System.out.println(x + " " + y); produces 10 20

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Memory Allocation for Parameters

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Memory Allocation for Parameters (cont'd)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Objects as parameters For an object passed to a method, the location of the object is copied into the parameter variable. –The object is not copied. Changes made to the object persist when the method returns A new object created inside the method will not be seen from the calling method.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Fraction class aka RationalNumber class To represent a fraction, need –numerator –denominator (should never be 0) Operations needed –accessors –mutators –simplify –arithmetic

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Returning an Object from a Method Methods can return objects as well as primitive values What gets returned is a reference (or an address) to the object. –The object is not copied

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sample Object-Returning Method Here's a sample method that returns an object: public Fraction simplify( ) { Fraction simp; int num = getNumerator(); int denom = getDenominator(); int gcd = gcd(num, denom); simp = new Fraction(num/gcd, denom/gcd); return simp; } Return type indicates the class of an object we're returning from the method. Return an instance of the Fraction class

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter A Sample Call to simplify

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter A Sample Call to simplify (cont'd)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Reserved Word this How does a method know where to find the data for the object it is called with? –The reserved word this is a reference to that object. this is called a self-referencing pointer because it refers to an object from that object's method. : Object this

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Uses of this The reserved word this can be used in several different ways. – To access other methods and instance variables within an instance method This use is usually optional. It is implied in a method call that is not qualified with an object name this can be used to disambiguate an instance variable from a local variable –To allow a constructor to call a different constructor –To allow a method to pass a reference to its object to another method We'll see examples when we start looking at the GUI classes

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using this to Refer to Other Members Methods public class Fraction { private int numerator, denominator; public void simplify() { int num, den, gcd; num = this.getNumerator(); den = this.getDenominator(); gcd = gcd( num, den); return new Fraction( num/gcd, den/gcd); }... } Data members public class Person { int age; public void setAge(int val) { this.age = val; }... }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter f1.add(f2) f2.add(f1) This time, we're calling f2's method, so the reserved word this is referring to f2. Because f1 is the receiving object (we're calling f1's method), so the reserved word this is referring to f1.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using this to Disambiguate Consider the following class definition public class Fraction { private int numerator, denominator; public Fraction( int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; }... } Without the this, all references to numerator and denominator would be to the parameter variables

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Constructors and this Use this to call a constructor from another constructor of the same class Must be the first statement in the constructor public Fraction( ) { //creates 0/1 this(0, 1); } public Fraction(int number) { //creates number/1 this(number, 1); } public Fraction(Fraction frac) { //copy constructor this(frac.getNumerator(), frac.getDenominator()); } public Fraction(int num, int denom) { setNumerator(num); setDenominator(denom); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Class Methods We use the reserved word static to define a class method. public static int gcd(int m, int n) { //the code implementing the Euclidean algorithm } public static Fraction min(Fraction f1, Fraction f2) { //convert to decimals and then compare } public static UnitsConverter readConverter(Scanner in) { //the code that reads data and returns a UnitsConverter } From a different class, static methods are called using the name of the class instead of the name of an object.