DEV Community

Cover image for From Java Methods to Method Overloading: A Complete Beginner's Guide to Compile-Time Polymorphism
Kathirvel S
Kathirvel S

Posted on

From Java Methods to Method Overloading: A Complete Beginner's Guide to Compile-Time Polymorphism

Introduction

When most beginners start learning Java, they spend a lot of time understanding variables, data types, classes, and objects. While these concepts are important, there is another concept that gives life to an object: methods.

Imagine you have a mobile phone.

The phone contains data:

  • Brand
  • Model
  • Storage
  • Battery Percentage

But simply having data is not enough.

A phone can also perform actions:

  • Make a call
  • Send a message
  • Open an application
  • Take a picture

In Object-Oriented Programming (OOP), data is represented using variables, while actions are represented using methods.

Without methods, objects would simply hold information. They would not be able to perform any meaningful work.

According to Oracle's Java Documentation, a method is a collection of statements that perform a specific operation. In simple terms, a method is a reusable block of code that performs a particular task.

In this article, we will take a complete journey through:

  • Objects and non-static variables
  • Accessing methods through objects
  • Understanding what methods are
  • Learning every keyword used in method creation
  • Return types and the return keyword
  • Parameters and arguments
  • Common compile-time errors
  • Method Overloading
  • Compile-Time Polymorphism

By the end of this article, you will understand not only how methods work but also why Method Overloading is called Compile-Time Polymorphism.

Let's begin from the very beginning.


Objects Access Non-Static Variables

Before understanding methods, we must understand how objects interact with variables.

Consider the following class:

class Student {

    String name = "Rahul";

}
Enter fullscreen mode Exit fullscreen mode

Here:

String name = "Rahul";
Enter fullscreen mode Exit fullscreen mode

is called an instance variable or non-static variable.

Official Definition

According to Oracle Java Documentation, instance variables belong to an object instance. Every object created from a class gets its own copy of instance variables.

Beginner-Friendly Explanation

Think of a class as a blueprint and an object as the real thing created from that blueprint.

Example:

  • Class → Student
  • Object → Rahul, Priya, Arjun

Every student object can have its own name.

To access an instance variable, we need an object.

public class Main {

    public static void main(String[] args) {

        Student s = new Student();

        System.out.println(s.name);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output:

Rahul
Enter fullscreen mode Exit fullscreen mode

Notice:

s.name
Enter fullscreen mode Exit fullscreen mode

This follows the pattern:

object.variable
Enter fullscreen mode Exit fullscreen mode

The object s is being used to access the variable name.

Common Beginner Error

System.out.println(name);
Enter fullscreen mode Exit fullscreen mode

Error:

cannot find symbol
Enter fullscreen mode Exit fullscreen mode

Why?

Because name belongs to the object, not directly to the main method.

Fix:

System.out.println(s.name);
Enter fullscreen mode Exit fullscreen mode

Key takeaway:

Non-static variables are accessed using object.variable.


Objects Can Also Access Methods

Objects do not only access variables.

They can also access methods.

Let's create a simple method.

class Student {

    void study() {

        System.out.println("Student is studying");

    }

}
Enter fullscreen mode Exit fullscreen mode

Create an object:

Student s = new Student();
Enter fullscreen mode Exit fullscreen mode

Call the method:

s.study();
Enter fullscreen mode Exit fullscreen mode

Output:

Student is studying
Enter fullscreen mode Exit fullscreen mode

This follows the pattern:

object.method();
Enter fullscreen mode Exit fullscreen mode

Real-World Analogy

Imagine a TV remote.

When you press a button, an action happens.

Similarly:

s.study();
Enter fullscreen mode Exit fullscreen mode

asks the object to perform the behavior called study().

Methods are often called the behavior of an object.


What Happens If the Method Does Not Exist?

Suppose we write:

Student s = new Student();

s.play();
Enter fullscreen mode Exit fullscreen mode

But inside Student:

class Student {

    void study() {

    }

}
Enter fullscreen mode Exit fullscreen mode

There is no play() method.

Error:

cannot find symbol
symbol: method play()
location: variable s of type Student
Enter fullscreen mode Exit fullscreen mode

Why Does This Error Occur?

Java checks everything before running the program.

The compiler asks:

  • Does the class exist?
  • Does the object exist?
  • Does the method exist?

Since play() cannot be found, compilation fails.

Important Rule:

A method must be declared before it can be called.


What Exactly Is a Method?

Oracle Documentation defines a method as:

A collection of statements grouped together to perform an operation.

Beginner-Friendly Definition

A method is a reusable block of code that performs a specific task.

Examples:

  • Calculating salary
  • Printing information
  • Finding the largest number
  • Sending an email

Without methods:

System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
Enter fullscreen mode Exit fullscreen mode

With methods:

greet();
greet();
greet();
Enter fullscreen mode Exit fullscreen mode

Methods help achieve:

  • Reusability
  • Readability
  • Maintainability
  • Modularity

Creating Your First Method

Example:

public void printActivity() {

    System.out.println("Learning Java");

}
Enter fullscreen mode Exit fullscreen mode

This method prints a message.

But what does each keyword mean?

Let's break it down.


Understanding Every Keyword in a Method

public

public
Enter fullscreen mode Exit fullscreen mode

Official Meaning

An access modifier that makes a member accessible from anywhere.

Beginner-Friendly Meaning

Anyone can use this method.

Think of it as an open door.

Example:

public void printActivity()
Enter fullscreen mode Exit fullscreen mode

means:

"This method can be accessed by other classes."


void

void
Enter fullscreen mode Exit fullscreen mode

Official Meaning

The method returns no value.

Beginner-Friendly Meaning

The method performs work but gives nothing back.

Example:

void greet() {

    System.out.println("Hello");

}
Enter fullscreen mode Exit fullscreen mode

It prints something but does not return any value.


printActivity

printActivity
Enter fullscreen mode Exit fullscreen mode

This is the method name.

Java uses the method name to identify which method to execute.

Good method names should describe the action being performed.

Examples:

calculateSalary()
sendEmail()
printActivity()
Enter fullscreen mode Exit fullscreen mode

Bad examples:

abc()
xyz()
doit()
Enter fullscreen mode Exit fullscreen mode

Parentheses ()

()
Enter fullscreen mode Exit fullscreen mode

Used to hold parameters.

Currently:

()
Enter fullscreen mode Exit fullscreen mode

means no parameters.


Curly Braces {}

{
}
Enter fullscreen mode Exit fullscreen mode

The method body.

The actual statements execute inside these braces.


Return Types in Java

Every method must specify what type of value it returns.

Example:

public int getAge() {

    return 25;

}
Enter fullscreen mode Exit fullscreen mode

Here:

int
Enter fullscreen mode Exit fullscreen mode

means the method returns an integer.

Why Does Java Need a Return Type?

Before compilation, Java must know:

"What kind of value will this method send back?"

That's why return types are mandatory.


Error: Missing Return Type

Wrong:

public getAge() {

    return 25;

}
Enter fullscreen mode Exit fullscreen mode

Error:

invalid method declaration; return type required
Enter fullscreen mode Exit fullscreen mode

Why?

Java does not know what type of value will be returned.

Fix:

public int getAge() {

    return 25;

}
Enter fullscreen mode Exit fullscreen mode

Understanding the return Keyword

The keyword:

return
Enter fullscreen mode Exit fullscreen mode

sends a value back to the caller.

Example:

public int getAge() {

    return 25;

}
Enter fullscreen mode Exit fullscreen mode

Usage:

int age = getAge();

System.out.println(age);
Enter fullscreen mode Exit fullscreen mode

Output:

25
Enter fullscreen mode Exit fullscreen mode

Flow:

Method Call
    ↓
Method Executes
    ↓
return 25
    ↓
Caller Receives 25
Enter fullscreen mode Exit fullscreen mode

Why Do We Use void?

Consider:

void greet() {

    System.out.println("Hello");

}
Enter fullscreen mode Exit fullscreen mode

The method simply performs an action.

Nothing needs to be returned.

Therefore:

void
Enter fullscreen mode Exit fullscreen mode

is used.

Rule:

Do work only → void

Return value → int, String, double, boolean, etc.
Enter fullscreen mode Exit fullscreen mode

Parameters and Arguments

This is one of the most commonly confused topics.

Parameter

void greet(String name)
Enter fullscreen mode Exit fullscreen mode

name is a parameter.

Argument

greet("Rahul");
Enter fullscreen mode Exit fullscreen mode

"Rahul" is an argument.

Simple rule:

Method Definition → Parameters

Method Call → Arguments
Enter fullscreen mode Exit fullscreen mode

Think of parameters as placeholders and arguments as actual values.


No-Argument Method

Method:

void greet() {

    System.out.println("Hello");

}
Enter fullscreen mode Exit fullscreen mode

Call:

greet();
Enter fullscreen mode Exit fullscreen mode

Output:

Hello
Enter fullscreen mode Exit fullscreen mode

Error: Passing an Argument to a No-Argument Method

Method:

void greet() {

}
Enter fullscreen mode Exit fullscreen mode

Call:

greet("Rahul");
Enter fullscreen mode Exit fullscreen mode

Error:

method greet cannot be applied to given types
Enter fullscreen mode Exit fullscreen mode

Fix:

void greet(String name)
Enter fullscreen mode Exit fullscreen mode

One-Parameter Method

Method:

void greet(String name) {

    System.out.println("Hello " + name);

}
Enter fullscreen mode Exit fullscreen mode

Call:

greet("Rahul");
Enter fullscreen mode Exit fullscreen mode

Output:

Hello Rahul
Enter fullscreen mode Exit fullscreen mode

Error: Missing Required Argument

Method:

void greet(String name)
Enter fullscreen mode Exit fullscreen mode

Call:

greet();
Enter fullscreen mode Exit fullscreen mode

Error:

method greet cannot be applied to given types
Enter fullscreen mode Exit fullscreen mode

Why?

The method expects one argument but receives none.

Fix:

greet("Rahul");
Enter fullscreen mode Exit fullscreen mode

Two-Parameter Method

Method:

void add(int a, int b) {

    System.out.println(a + b);

}
Enter fullscreen mode Exit fullscreen mode

Call:

add(10, 20);
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

Error: Passing One Argument Instead of Two

Call:

add(10);
Enter fullscreen mode Exit fullscreen mode

Error:

method add cannot be applied to given types
Enter fullscreen mode Exit fullscreen mode

Fix:

add(10, 20);
Enter fullscreen mode Exit fullscreen mode

String Parameters

Method:

void display(String name) {

    System.out.println(name);

}
Enter fullscreen mode Exit fullscreen mode

Correct:

display("Java");
Enter fullscreen mode Exit fullscreen mode

Output:

Java
Enter fullscreen mode Exit fullscreen mode

Error: Passing Wrong Data Type

Method:

display(String name);
Enter fullscreen mode Exit fullscreen mode

Call:

display(100);
Enter fullscreen mode Exit fullscreen mode

Error:

incompatible types
Enter fullscreen mode Exit fullscreen mode

Reason:

Method expects:

String
Enter fullscreen mode Exit fullscreen mode

but receives:

int
Enter fullscreen mode Exit fullscreen mode

Fix:

display("100");
Enter fullscreen mode Exit fullscreen mode

Why Method Overloading Exists

Imagine a printing system.

Without overloading:

printInt()
printString()
printDouble()
printTwoNumbers()
Enter fullscreen mode Exit fullscreen mode

Too many method names.

Difficult to remember.

Java provides a cleaner solution.


Method Overloading

Official Definition

Method Overloading is the ability to define multiple methods with the same name but different parameter lists.

Example:

class Printer {

    void print() {
        System.out.println("No Data");
    }

    void print(int num) {
        System.out.println(num);
    }

    void print(String text) {
        System.out.println(text);
    }

    void print(int a, int b) {
        System.out.println(a + b);
    }

}
Enter fullscreen mode Exit fullscreen mode

Calls:

print();

print(10);

print("Java");

print(10, 20);
Enter fullscreen mode Exit fullscreen mode

Output:

No Data
10
Java
30
Enter fullscreen mode Exit fullscreen mode

Notice:

Same method name.

Different parameter lists.

That is Method Overloading.


Rules of Method Overloading

Valid:

print()
print(int)
print(String)
print(int, int)
Enter fullscreen mode Exit fullscreen mode

Invalid:

int print()

String print()
Enter fullscreen mode Exit fullscreen mode

Changing only the return type is not overloading.

The parameter list must be different.


What Is Compile-Time Polymorphism?

The word polymorphism comes from:

Poly = Many

Morph = Forms
Enter fullscreen mode Exit fullscreen mode

Meaning:

One Name
Many Forms
Enter fullscreen mode Exit fullscreen mode

Example:

print()
print(int)
print(String)
print(int, int)
Enter fullscreen mode Exit fullscreen mode

All methods share the same name.

But each has a different form.


Why Is Method Overloading Called Compile-Time Polymorphism?

Consider:

print(10);
Enter fullscreen mode Exit fullscreen mode

During compilation, Java checks:

void print(int num)
Enter fullscreen mode Exit fullscreen mode

For:

print("Java");
Enter fullscreen mode Exit fullscreen mode

Java checks:

void print(String text)
Enter fullscreen mode Exit fullscreen mode

The decision happens before the program runs.

That means the compiler determines which method should execute.

Therefore:

Method Overloading
        ↓
Method Selected During Compilation
        ↓
Compile-Time Polymorphism
Enter fullscreen mode Exit fullscreen mode

It is also called:

Static Polymorphism
Enter fullscreen mode Exit fullscreen mode

because the method resolution happens during compile time rather than runtime.


Final Summary

In this article, we started with the fundamentals of objects and non-static variables. We learned how objects access data using the dot operator and how methods represent the behavior of an object.

We explored what methods are, why they exist, and how they help us write reusable and maintainable code. We broke down every component of a method, including public, void, method names, parentheses, and method bodies.

We then moved into return types and the return keyword, understanding how methods can send values back to the caller. After that, we learned the difference between parameters and arguments and examined common compile-time errors that beginners frequently encounter.

Finally, we reached Method Overloading. Instead of creating many methods with different names, Java allows us to use the same method name for multiple operations by changing the parameter list.

This feature is called Method Overloading, and because Java determines the correct method during compilation, it is known as Compile-Time Polymorphism.

The most important takeaway from this entire journey is:

Method Overloading allows one method name to have multiple forms, and Java selects the correct form during compilation. This ability is what makes Method Overloading an example of Compile-Time Polymorphism.

Understanding methods thoroughly is one of the strongest foundations you can build as a Java developer. Almost every Java application—from simple console programs to enterprise-level systems—relies heavily on methods. Mastering them today will make advanced topics such as inheritance, runtime polymorphism, interfaces, and design patterns much easier to understand in the future.

Happy Coding!

References

  1. Oracle Java Tutorials – Methods
    https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

  2. Oracle Java Tutorials – Objects
    https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html

  3. Java Language Specification – Methods
    https://docs.oracle.com/javase/specs/

  4. Oracle Java Tutorials – Passing Information to a Method or Constructor
    https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

Top comments (0)