Method, Method Overloading,
Method Overriding
1
Prepared By:
Aditi Nandi Tokder
Assistant Professor
ECE Department
Techno Main Salt Lake
Introduction
1
2
Method Overloading
4
Types3
Advantages of Method Overloading
5
4
5
6
7
3
Slide
Declaration, Signature and Syntax
Method Overriding
6
8
2
Example
7
9
Introduction
3
What is a Method?
In the context of web development, a method is a block of code that performs a specific task. Imagine a method
as a mini-program within a larger program. It's a collection of instructions that, when called upon, execute a
specific sequence of operations. Methods are fundamental to object-oriented programming (OOP) and play a
crucial role in organizing and managing code.
Reusability
Methods promote code reusability. Instead of repeating code blocks, developers can define a method and call it
whenever needed.
Modularity
Methods break down complex tasks into smaller, manageable units, making code more organized and easier to
understand.
Abstraction
Methods hide implementation details, allowing developers to focus on what the method does, rather than how it
does it.
The most important method in Java is the main() method.
Declaration, Signature and Syntax
4
The method declaration provides information about
method attributes, such as visibility, return-type, name, and
arguments. It has six components that are known as
method header.
Method Name
The method name is a descriptive identifier that reflects the method's purpose. For example, a method that
calculates the area of a rectangle could be named "calculateArea."
Parameters
Parameters are variables that hold the data the method receives. They act as input for the method to operate on.
The types of parameters determine what kind of data the method can handle.
Return Type
The return type indicates the type of data the method produces and returns to the caller. It might be a number, a
string, a boolean value, or even an object.
SYNTAX: accessModifier returnType methodName(parameterList) {
// method body
}
Types :-
5
There are two main types of methods : (1) Predefined Method (2) User-defined Method
Predefined Methods
Predefined methods are methods that are already defined in Java libraries. These methods are part of the
Java standard library and are available for use in Java programs. They simplify complex tasks by providing
ready-to-use functionality. Predefined methods are also referred to as built-in methods or standard library
methods.
Examples:
System.out.println(): Prints a message to the console.
Math.max(int a, int b): Returns the greater of two integer values.
String.length(): Returns the length of a string.
User-defined Methods
User-defined methods are methods that are created by the programmer to perform specific tasks. These
methods are defined within the user's class and can be customized to meet the specific needs of the
program. User-defined methods improve code readability and reusability by breaking down complex tasks
into smaller, manageable functions.
Method Overloading
6
Method overloading is a powerful technique that allows you to create multiple methods with the same
name but different parameters. This feature enhances code flexibility and readability by reducing the
need for similar but differently named methods.
Same Name, Different Parameters
The key principle of method overloading is having multiple methods with the same name but accepting
different parameter types, numbers, or orders. This allows you to call the same method with different sets
of data, leading to more concise and reusable code.
Implicit Resolution
When you call an overloaded method, the compiler automatically determines which method to execute
based on the types and order of the arguments you pass. This process is known as "overloading
resolution."
Example: "calculateArea"
Consider a method called "calculateArea". You could have one version that calculates the area of a
rectangle by taking its length and width as parameters. Another version could calculate the area of a
circle, taking only its radius as input.
Advantages of Method Overloading
7
Reduces the need for multiple, similar methods with slightly different names. Overloading allows
you to use the same method name for different scenarios, making the code more concise and easier
to understand.
Method overloading provides flexibility in handling different input data types and combinations. You
can call the same method with different types of data, ensuring the code adapts to various situations.
By reducing code repetition and creating a consistent naming scheme, overloading simplifies code
structure and makes it less prone to errors. It promotes a clear and organized approach to code
development.
Increased Code Reusability
Enhanced Flexibility
Reduced Code Complexity
8
Method Overriding
Method overriding in Java is a feature that allows a subclass to provide a specific implementation for a
method that is already defined in its superclass. When a method in a subclass has the same name,
return type, and parameters as a method in the superclass, the method in the subclass is said to
override the method in the superclass.
Inheritance
Overriding takes place in the context of inheritance. When a subclass inherits from a parent class, it
inherits all the methods and properties of the parent class. Overriding allows the subclass to modify the
behavior of inherited methods.
Polymorphism
Method overriding supports polymorphism, meaning that the same method can behave differently
depending on the object it's called on. This dynamic behavior is a fundamental aspect of OOP and allows
for flexible and adaptable code.
Redefining Behavior
When a subclass overrides a method, it provides a new implementation that replaces the inherited
version. This allows the subclass to customize the method's behavior according to its specific needs.
9
Example:-
class Animal {
// Method in the superclass
public void makeSound() {
System.out.println("Animal makes a sound"); }}
class Dog extends Animal {
// Overriding the makeSound method in the subclass
@Override
public void makeSound() {
System.out.println("Dog barks"); }}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create an Animal object
Animal myDog = new Dog(); // Create a Dog object (polymorphism)
myAnimal.makeSound(); // Calls the method from Animal class
myDog.makeSound(); // Calls the overridden method from Dog class
}
}