Is it possible to have two methods within the same class that have the same name and parameter lists but which have different return types?

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.

  1. statement A calls processDeposit(int)
  2. statement B calls processDeposit(int, int)

Method Signature

When several methods have the same name, only one is picked by a method call. The types of the actual parameters in a method call are matched with the types of the formal parameters of the methods. If an exact match cannot be made, then the actual parameters are converted to types that match the formal parameters if this can be done without potential loss of information.

For example, the call

bobsAccount.processDeposit( 200, 25 ); //statement A

matches this method declaration:

public void

processDeposit( int amount, int serviceCharge )

because the number and types of the actual parameters matches the number and types of the formal parameters.

The signature of a method is:

      • Its name
      • The number and types of its parameters, in order

The signatures of the methods in a class must be unique. For example, the signatures of the two processDeposit methods are:

      • processDeposit( int )
      • processDeposit( int, int )

The names of the parameters are not part of the signature because parameter names are not visible outside of their scope.

The return type is not part of the signature. The visibility modifier is not part of the signature.


Say that a class has the following two methods:

    float chargePenalty( int amount ) { ... } int chargePenalty( int penalty ) { ... }

Do these methods have unique signatures?

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.

      • 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.

      • 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 Recent activity

No recent activity

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!

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

        • describe the history of software and computer design;
        • explain formal logic and how it is a subset of human logic;
        • describe the relationship between human logic, computers, and computer languages;
        • describe the basic computer model;
        • explain the programming life cycle;
        • discuss the history of the Java programming language;
        • set up a Java Development Kit (JDK) for Java development;
        • set up a NetBeans IDE for Java programming;
        • write and run a simple Java program; and
        • explain how computers are a tool to assist people and describe appropriate and inappropriate uses of computers.

        • Computer History Book

          These articles give some insight into the early history of computers, and introduce the powerful ideas that enabled the computer architecture of our day and that will influence computer architecture of tomorrow.

        • The History of Computing Hardware (1960-Present) Page

          Read this article to supplement the previous articles. As you see here, the history of computers is split into "generations". This course will primarily focus on the third generation of computers, which began in the 1960s, and the microcomputer technology of today, which has meant that computers are now present in the homes of many people across the world.

        • Tally Sticks and the Abacus Page

          Lest you get the impression that computers are a new phenomena, this short article demonstrates that computers, in one form or another, have been around since at least 18,000BC.

        • The History of Programming Languages Page

          This article focuses on the evolution of programming languages over the years. How did programming languages change as the "generations" of computers advanced?

        • A Concise Introduction to Logic Book

          Aristotle lived in ancient Greece around 384-322 BC. He is credited with the invention of a field of philosophy called "formal logic". This is a way of thinking that only admits to something being absolutely true or absolutely false. For instance, an item is in one group or some other group but it can not be a member of more than one group at a time. Everything is black or white. There are no shades of gray. You may have heard this called "binary thinking". However, it is perfect for computers because computers are nothing more than black boxes filled with on/off switches that are either completely on or completely off. As such, there is nothing new about computers as we know them today. They simply allow us to mechanize complex constructs in formal logic, and to do so at very high speeds. Gottfried Leibniz (1700s) created a means of using binary values to perform arithmetic. Essentially, this is the translation of our usual Base 10 numeric values to Base 2 (binary) values, and the means to perform arithmetic operations on such values. George Boole (1815-1864) invented Boolean Algebra, a mathematical means of expressing and manipulating formal-logic variables using logical operators to get correct results in highly complex situations. So, briefly, you can see the philosophical and mathematical history that underlies modern computers. This history, and the mechanization of that history by computers, allows human thought to be reflected and carried out consistently, although not perfectly. (Computers can not even perform the operation 1/3 with absolute accuracy.) For this introductory course, it is enough to give some thought to formal logic and its basic ideas. Read these two chapters.

        • An Introduction to Formal Logic File

          Supplement what you just read by reading the first chapter of this book. As you read, you will see the careful human thought that is required to create the logical constructs required to get the computer to do anything worthwhile. Section 1.6 begins the process of creating a formal syntax, or language, from human statements. In this course, we use the formal language Java to translate human statements into something that can be translated into the 1's and 0's the computer works with, the settings of the computer's electronic switches.

        • Innovation and Its Application File

          One definition of innovation is the process by which solutions are created for complex problems. Another perspective sees innovation as creating something entirely new that takes situations and people down a path not previously conceived. Read this section, which discusses innovation and several practical aspects of bringing new ideas to fruition, and demonstrates case studies of successful applications.

        • Using computers more widely has benefited society greatly. However, computers have their limitations, as do we humans. For all the hype about "intelligent" computers, it is humans who create computers, who put data into computers, who write programs that process that data, and who select and interpret the results of that processing. Clearly, the "intelligence" of computers comes directly from the humans who create and use them. Thus, we can never say, "Oh, It was just the computer. It made a mistake." Spend some time thinking about that idea before you move forward

        • Introduction to Computer Systems Book

          Review these notes. For maximum benefit, go through these notes interactively, thinking about and answering the questions at the bottom of each page. These notes are an experiment in applying the "programmed learning" method to web-based computer aided instruction. The subject is Java Programming for beginning programmers. The content is intended to start beginning programmers out on the track to professional-level programming and reinforce learning by providing abundant feedback. Java is a programming language that is used often in professional practice.

        • The Processor Book

          Review these notes. As you work through them, think about and answer the questions at the bottom of each page.

        • Introduction to Number Systems and Binary Page

          Watch this lecture on binary numbers, which are used to represent numbers in computer memory. In fact, all types of information, not just numbers, but characters as well, are stored in memory using binary bits (that is, digits) of 0 and 1. We still write numbers in our programs using decimal numbers, but the interpreter converts them to binary when it interprets them for execution as part of a machine language instruction (the interpreter interprets the Java statement to equivalent machine language statements).

        • Quiz on Computer Systems and the Processor

          Take this short quiz to see how well you understand what we've covered so far.

          • This quiz 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 quiz as many times as you want, whenever you want.

        • The Programming Lifecycle Page

          Read this brief article about the programming lifecycle. As you read, consider the following steps of the programming lifecycle: Planning and Analysis, Design, Implementation, Testing and Debugging, Deployment, Maintenance

        • Comparing Waterfall, Unified, and Agile Software Development Processes Page

          Three major approaches to software development, which are the means of carrying out the programming lifecycle, have been tried by practitioners. This article compares those three. The most used today are Unified and Agile. These processes help to guide software development projects. They are not software development in and of themselves. Always keep in mind that the goal is to deploy excellent software, not to follow processes.

        • A Top-Level View of Java Page

          Java is both a compiled and interpreted language. What you write as Java code is compiled into machine-independent bytecode. The bytecodes are interpreted for the particular machine the bytecodes are running on. In this way, Java becomes machine independent. Such is not the case for languages like C/C++, where the language code is compiled into assembly code for a particular machine. A linker turns that code into an executable for that machine.

        • Downloading and Installing JDK Book

          This page shows how to download and install the latest version of the Java Development Kit (JDK). Read the instructions carefully to set the "classpath" mentioned in Step 3. Once JDK has been installed, you can write a simple Java program using an editor such as notepad and run it from a command prompt. Alternatively, Java programs can be written using an Integrated Development Environment (IDE) such as NetBeans, described below.

        • Downloading and Installing NetBeans IDE Book

          These instructions describe how to download and install NetBeans, a commonly used IDE for Java programming. Using an IDE means that you have all of the tools you need in one place (your "development environment") instead of having to organize things manually. Use the instructions in Step 2 to write a simple Java program called "Hello.java" and then compile and run it.

        • Introduction to Java Book

          Work through these slides. As you read, think about and answer the questions at the bottom of each page. These will be your first experience with Java, so make sure you follow each step closely.

        • Introduction to Java – Practice URL

          Think of the word that should fill in the blank for each statement. Click on each one to see what the right answer should be.

        • Unit 1 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 Skip Recent activity

No recent activity

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!

Postingan terbaru

LIHAT SEMUA