Fundamentals 13 min read

Comprehensive Guide to Java Methods: Types, Modifiers, Overloading & Overriding

This article systematically outlines the six-step process for defining complete Java class methods, covering access modifiers, static and abstract keywords, return types, naming conventions, parameter lists, method bodies, as well as method overloading, overriding, and various invocation techniques.

ITPUB
ITPUB
ITPUB
Comprehensive Guide to Java Methods: Types, Modifiers, Overloading & Overriding

Defining a complete Java class method can be broken down into six major steps, which together generate nine distinct method categories. The author presents a visual summary and invites corrections.

Step 1 – Access Modifiers

Choose the appropriate visibility (public, protected, private, or default) based on the earlier discussion of member and local variables.

Step 2 – Keywords (static & abstract)

Static methods belong to the class itself and can be called without an instance; they lack a this reference. Common static methods include main. Benefits include single initialization, reduced memory usage, and easier testing.

Abstract methods are declared with the abstract keyword, have no body, and must be implemented by a subclass. The containing class becomes abstract and cannot be instantiated.

Step 3 – Return Type

If a method returns a value, specify the concrete type (e.g., String, int) and ensure a matching return statement. Void methods use the void keyword and typically perform output directly.

public String display(){
    String aa = "影片名字:" + name + "
类型:" + type + "
导演:" + daoyan + "
主演:" + zhuyan;
    return aa;
}

Calling the method:

System.out.println(f.display());

Step 4 – Method Name

Choose a meaningful, English‑style name; use camelCase for multi‑word names. Constructors share the class name and have no return type.

Step 5 – Parameter List

Parameters allow methods to receive external data. Examples:

public void sleep(){
    System.out.println("我在睡觉");
}

public void sleep(String i){
    System.out.println("我在" + i + "睡觉");
}

public void sleep(String i, int j){
    System.out.println("我在" + i + "睡觉" + j + "点起来");
}

Methods can be categorized by the presence or absence of parameters and return values, yielding combinations such as "no‑arg void", "arg void", "no‑arg with return", etc.

Step 6 – Method Body

The body contains the executable statements. It may be empty (e.g., abstract methods) or consist of a single statement.

Method Overloading

Overloading occurs when multiple methods share the same name but differ in parameter count, type, or order. This reduces the need for distinct method names and leverages Java’s strong typing.

Method Overriding

Overriding happens in inheritance hierarchies: a subclass provides its own implementation of a method defined in a superclass, allowing polymorphic behavior.

Method Invocation Styles

Direct calls within the same class (subject to access modifiers).

Indirect calls using this (same class) or super (parent class).

Calls on an instance of another class after creating the object with new.

Static imports such as import java.util.*; to use utility methods.

The author notes that the summary took many hours and may still miss some details, inviting further feedback.

Source: 麦穗科技

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programming fundamentalsmethodsstaticoverloadingoverridingabstract
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.