What is the process of defining two or more methods within the same class having the same name but different parameters declaration ?*?

This chapter reviews method parameters and local variables, as well as method overloading and method signature.

Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters. When a method is called, the corresponding method is invoked by matching the arguments in the call to the parameter lists of the methods. The name together with the number and types of a method's parameter list is called the signature of a method. The return type itself is not part of the signature of a method.

Two Objects

Colors in the following show how this works. this is used in the constructor where the instance variable, not the parameter, should be used.

class Mystery{

private int sum;

public Mystery( int sum )

{

this.sum = sum;

} public void increment( int inc ) {

sum = sum + inc;


System.out.println("Mystery sum: " + sum ); }}public class Tester{ public static void main ( String[] args) {

int sum = 99;

Mystery myst = new Mystery( 34 ); myst.increment( 6 );

System.out.println("sum: " + sum );

} }

Now look at this modified version:

class Mystery{ private int sum; public Mystery( int x ) { sum = x; } public void increment( int inc ) { sum = sum + inc; System.out.println("Mystery sum: " + sum ); }}public class Tester{ public static void main ( String[] args) { Mystery mystA = new Mystery( 34 ); Mystery mystB = new Mystery( 13 ); mystA.increment( 6 ); mystB.increment( 7 ); } }


Skip Table of contents
Skip Activities


Page 2

Learn new skills or earn credit towards a degree at your own pace with no deadlines, using free courses from Saylor Academy. We're committed to removing barriers to education and helping you build essential skills to advance your career goals. Start learning here, or check out our full course catalog.

Log in or Sign up to enroll in courses, track your progress, gain access to final exams, and get a free certificate of completion!

    • Upon successful completion of this unit, you will be able to:

      • write methods with zero or more parameters; and
      • make method calls.

      • What is the process of defining two or more methods within the same class having the same name but different parameters declaration ?*?
        Methods: Communicating with Objects Book

        We communicate with objects using methods. Methods are executable code within each object, for which an interface has been established. Sometimes the interface is only for the object itself. Other times it is an interface accessible by other objects. This chapter discusses that topic in detail.

      • Threads and Concurrent Programming Book

        Threads may be seen as methods that execute at "the same time" as other methods. Normally, we think sequentially when writing a computer program. From this perspective, only one thing executes at a time. However, with today's multi-core processors, it is possible to literally have several things going on at the very same time while sharing the same memory. There are lots of ways that this is done in the real world, and this chapter goes over them in a way that you can apply to your own projects.

      • Parameters, Local Variables, and Overloading Book

        This chapter reviews method parameters and local variables, as well as method overloading and method signature.

        Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters. When a method is called, the corresponding method is invoked by matching the arguments in the call to the parameter lists of the methods. The name together with the number and types of a method's parameter list is called the signature of a method. The return type itself is not part of the signature of a method.

      • What is the process of defining two or more methods within the same class having the same name but different parameters declaration ?*?
        Unit 6 Assessment Quiz

        Take this assessment to see how well you understood this unit.

        • This assessment does not count towards your grade. It is just for practice!
        • You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
        • You can take this assessment as many times as you want, whenever you want.

Skip Activities
Skip Discuss Computer Science


Page 3

Learn new skills or earn credit towards a degree at your own pace with no deadlines, using free courses from Saylor Academy. We're committed to removing barriers to education and helping you build essential skills to advance your career goals. Start learning here, or check out our full course catalog.

Log in or Sign up to enroll in courses, track your progress, gain access to final exams, and get a free certificate of completion!

Skip Activities
Skip Discuss Computer Science


Page 4

Learn new skills or earn credit towards a degree at your own pace, with no deadlines, using free courses from Saylor Academy. We're committed to removing barriers to education and helping you build essential skills to advance your career goals. Choose a course below, or check out our full course catalog.

Log in or Sign up to enroll in courses, track your progress, gain access to final exams, and get a free certificate of completion!