Advanced Object-Oriented Programming

Syntax notes note that these calls are sent to

This preview shows page 4 - 7 out of 9 pages.

Syntax Notes: Note that these calls are sent to odometer , and no other object (which is another reason that this is referred to as a "message"). For example, if we had constructed another Sum called somethingElse : Sum odometer(), somethingElse(); then a call of the form: somethingElse.add(86); would only affect the total variable of somethingElse . It would have no affect on odometer . If a state variable (such as total ) is redeclared in a method, then references to that variable will affect the local variable only (sort of like what happens if you redeclare a global variable locally in C). For example: public void doNothing() { int total; // total is redeclared locally total = 0; // this only changes the local total }
Image of page 4

Subscribe to view the full document.

Overloaded Methods and Constructors One potential problem with the convention that the name of the constructor must be the same as the name of the class is that it might only allow us one way to initialize objects in the class – that is, while we may have many different methods in a class (such as add and getSum ), we can only have one constructor ( Sum ), since it must have the same name as the class. Suppose I wanted to give the user two different options for creating Sum objects. Specifically: A default constructor, which allows the user to create a Sum with an initial total of 0 (like the one given above). An additional constructor, which allowed the user to specify the initial total to something other than 0. That is, I would like to modify the above class as follows: // Sum is a class representing a running integer total. // Users can add to the total, and find the total. public class Sum { private int total; // Internal state variable // The default constructor sets the internal running total to 0. public Sum() { total = 0; } // The additional constructor sets the running total to the given // parameter. public Sum(int t) { total = t; } // The add method increments the running total. public void add(int x) { total = total + x; } // The getSum method returns the current value of the // running total. public int getSum() { return total; } }
Image of page 5
In the above example, I have overloaded the Sum constructor. Overloading a method or constructor refers to reusing the same name with more than one definition . In C, this would give us a compiler error, as you cannot redefine a function any more than you can redefine a variable. In Java, however, we can give the same constructor or method more than one definition, as long as it is possible to disambiguate between them based on either: The number of parameters The type of parameters For example, consider the following lines of code: Sum s1 = new Sum(); Sum s2 = new Sum(42); Java will choose the definition of Sum that matches the type and number of parameters. s1 would be created using the first constructor, and s2 would be created using the second constructor.
Image of page 6

Subscribe to view the full document.

Image of page 7
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern