How to call a method of different class in Java?

How to Call a Method of a Different Class in Java?

In Java, it is a common requirement to call a method of a different class. This technique is known as method overloading or method overriding, and it is used to reuse the code and provide more flexibility to the user. In this article, we will discuss how to call a method of a different class in Java and the different ways to achieve this.

Method Overloading in Java

In Java, method overloading is a technique where multiple methods with the same name can be declared in the same class, as long as they have different method signatures. This allows us to call different methods based on the parameters passed to them. Here is an example of method overloading:

public class Calculator {
public int add(int a, int b) {
return a + b;
}

public double add(double a, double b) {
return a + b;
}

public String add(String a, String b) {
return a + b;
}
}

In this example, we have created a Calculator class with three methods all with the same name add, but with different parameter types. We can call these methods by creating an instance of the Calculator class and calling the method that matches the parameters passed to it.

Method Overriding in Java

Method overriding is a technique where a subclass provides a specific implementation for a method that is already defined in its superclass. Here is an example of method overriding:

public class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}

public class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}

In this example, we have created a Dog class that extends the Animal class and overrides the sound() method. We can call the overridden method by creating an instance of the Dog class and calling the sound() method.

Calling a Method of a Different Class

To call a method of a different class, you can use one of the following methods:

• Direct Method Call: You can call a method of a different class by directly mentioning the class name and method name.

Calculator calculator = new Calculator();
calculator.add(2, 3);

• Method Reference: You can use a method reference to call a method of a different class.

Calculator calculator = new Calculator();
Calculator::add

• Lambda Expression: You can use a lambda expression to call a method of a different class.

Calculator calculator = new Calculator();
(int a, int b) -> calculator.add(a, b)

Best Practices

  • Use Method Overloading: Use method overloading to provide multiple methods with the same name but different parameters.
  • Use Method Overriding: Use method overriding to provide a specific implementation for a method in a subclass.
  • Use Method References: Use method references to call a method of a different class.
  • Use Lambda Expressions: Use lambda expressions to call a method of a different class.

Conclusion

In this article, we have discussed how to call a method of a different class in Java. We have covered method overloading, method overriding, and method referencing. We have also provided some best practices for calling a method of a different class. By following these best practices, you can make your code more efficient and easier to maintain.

Here is a summary of the main points:

Method Description
Direct Method Call Directly calling a method of a different class by mentioning the class name and method name.
Method Reference Using a method reference to call a method of a different class.
Lambda Expression Using a lambda expression to call a method of a different class.
Method Overloading Providing multiple methods with the same name but different parameters.
Method Overriding Providing a specific implementation for a method in a subclass.

I hope this article has been helpful in understanding how to call a method of a different class in Java.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top