Method Overloading and Overriding in Java (With Example)- Object-Oriented Programming

 

Method overloading and overriding in java


In this article, we'll take a look at the concepts of method overloading and overriding in Java. We'll start with a basic explanation of each concept, and then move on to some examples. By the end of this article, you should have a good understanding of how these two concepts work in Java.

Method Overloading In Java

Method overloading is one of the most important features of object-oriented programming in Java. It allows a programmer to create multiple methods with the same name, but with different signatures. This can be extremely useful when you need to perform the same action on different data types.

One simple example of method overloading is the println() method. This method is overloaded to print different data types, such as int, char, and String.

To overload a method in Java, you simply create multiple methods with the same name but with different signatures. The signature of a method includes the name of the method, the number and type of parameters, and the return type. For example, you could create two methods named print() that accept different numbers of parameters:

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

public void print(int x, int y) {
System.out.println(x + "," + y);
}

Example of Method Overloading

In Java, methods can be overloaded. This means that there can be multiple methods with the same name but with different parameter lists. When a method is invoked, the Java runtime system looks for a matching method based on the number of arguments and their types. If a match is found, the matching method is executed. Otherwise, an error occurs.

Overloading allows you to define methods with the same name but with different argument lists. When an overloaded method is invoked, the Java runtime system looks for a matching method based on the number of arguments and their types. If a match is found, the matching method is executed. Otherwise, an error occurs.

Here's an example of overloading methods:

public class OverloadExample {
public static void main(String[] args) {
int a = 10;
int b = 20;

OverloadExample obj = new OverloadExample();

obj.sum(a, b); // invoking the sum method with 2 arguments
obj.sum(a, b, c); // invoking the sum method with 3 arguments

}

public void sum(int a, int b)

Method Overriding In Java

As we know, object-oriented programming is all about classes and objects. And, one of the most important concepts in OOP is inheritance. Inheritance allows us to create relationships between classes so that we can reuse code and extend functionality.

Inheritance also plays a key role in method overriding. Method overriding occurs when a child class redefines a method from the parent class. The child class can provide its own implementation for the method, which may be different from the parent class's implementation.

One common use case for method overriding is to override the toString() method. The toString() method is used to convert an object into a string representation. By default, the toString() method just prints out the object's class name and memory address. However, we can override this method to provide a more meaningful string representation for our objects.

Override example:

public class Employee {
private String name;
private int id;

public String toString() {
return "Employee[name=" + name + ",id=" + id + "]";
}
}

Example of Method Overriding

When it comes to object-oriented programming, one of the most important concepts is that of inheritance. Inheritance allows us to create relationships between classes, wherein one class can inherit the properties and behavior of another class.

One way in which inheritance is manifested is through the concept of method overriding. Method overriding occurs when a child class redefines a method that was already defined in a parent class. When this happens, the child class's version of the method takes precedence over the parent class's version.

Let's take a look at an example of method overriding to see how this works in practice. We'll start with a simple parent class called Animal:

class Animal {
public void move() {
System.out.println("Animals can move!");
}
}
Now let's create a child class called Dog that inherits from Animal:

class Dog extends Animal {
@Override
public void move() {
System.out.println("Dogs can walk and run!");
}
}
As you can see, the Dog class has a method called move() which overrides the move() method from the Animal class. When we call

Difference Between Method Overloading & Overriding

Method overloading is a feature in Java that allows a class to have more than one method with the same name. The methods must have different parameter lists. Method overloading is often used to provide multiple ways to invoke a method.

Method overriding is a feature in Java that allows a subclass to provide a different implementation of a method defined in the superclass. When a method is overridden, the subclass redefines the method to provide its own implementation.

Why We Use Method Overloading

We use method overloading when we want to create multiple methods that perform similar actions. For example, let's say we have a class that represents a bank account. This class might have methods for deposit and withdrawal. We could have a separate method for each type of transaction (e.g., one for deposits and one for withdrawals), or we could use method overloading to create one method that can handle both types of transactions.

Method overloading is also helpful when we want to provide different levels of functionality. For example, let's say our bank account class has a method for transferring funds to another account. We could provide two overloaded versions of this method: one that transfers a specified amount and another that transfers the entire balance.

Overall, method overloading provides a way to add flexibility and variety to our code. It's a powerful tool that can be used in many different situations.

Why We Use Method Overriding

Method overriding is a powerful feature of object-oriented programming that allows you to change the behavior of a method in a child class. By overriding a method, you can provide a different implementation that is more suited to the needs of the child class.

One common use case for method overriding is to handle different cases in a more efficient way. For example, consider a parent class with a method that calculates the average of two numbers. The child class could override this method to calculate the average of three numbers, which would be more efficient in many cases.

Another common use case for method overriding is to change the behavior of a method based on the type of object that is passed as an argument. For example, consider a parent class with a method that sorts an array of numbers. The child class could override this method to sort an array of strings, which would be more appropriate in many cases.

Method overriding is a powerful tool that can help you achieve greater flexibility and efficiency in your object-oriented programs.

Code School

Code School is sharing assignments, books, labs, lectures, notes, OOP and java code, projects, and semester projects.

Post a Comment

Previous Post Next Post