Home Green Executing Methods Across Classes- A Comprehensive Guide to Java Inter-Class Method Invocation

Executing Methods Across Classes- A Comprehensive Guide to Java Inter-Class Method Invocation

by liuqiyue

How to Call a Method from a Different Class in Java

In Java, it is a common practice to organize code into different classes to achieve modularity and reusability. However, sometimes you may need to call a method from a different class. This can be achieved by using various techniques such as inheritance, interfaces, and reflection. In this article, we will explore different methods to call a method from a different class in Java.

1. Using Inheritance

One of the most straightforward ways to call a method from a different class is by using inheritance. By extending a class, you can access its methods and variables. Here’s an example:

“`java
class Parent {
public void display() {
System.out.println(“This is the parent class method.”);
}
}

class Child extends Parent {
public void callParentMethod() {
display(); // Calling the method from the parent class
}
}

public class Main {
public static void main(String[] args) {
Child child = new Child();
child.callParentMethod();
}
}
“`

In this example, the `Child` class extends the `Parent` class. The `callParentMethod()` method in the `Child` class calls the `display()` method from the `Parent` class.

2. Using Interfaces

Another way to call a method from a different class is by using interfaces. Interfaces define a contract that classes can implement. Here’s an example:

“`java
interface Animal {
void makeSound();
}

class Dog implements Animal {
public void makeSound() {
System.out.println(“Woof! Woof!”);
}
}

class Cat implements Animal {
public void makeSound() {
System.out.println(“Meow! Meow!”);
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();

callAnimalSound(dog); // Calling the method from the Dog class
callAnimalSound(cat); // Calling the method from the Cat class
}

public static void callAnimalSound(Animal animal) {
animal.makeSound();
}
}
“`

In this example, the `Animal` interface defines the `makeSound()` method. The `Dog` and `Cat` classes implement the `Animal` interface and provide their own implementations of the `makeSound()` method. The `callAnimalSound()` method takes an `Animal` object as a parameter and calls the `makeSound()` method on it.

3. Using Reflection

Reflection is a powerful feature in Java that allows you to inspect and manipulate classes, methods, and fields at runtime. You can use reflection to call a method from a different class dynamically. Here’s an example:

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

public class Main {
public static void main(String[] args) {
try {
Class calculatorClass = Class.forName(“Calculator”);
Object calculatorInstance = calculatorClass.getDeclaredConstructor().newInstance();

Method addMethod = calculatorClass.getMethod(“add”, int.class, int.class);
int result = (int) addMethod.invoke(calculatorInstance, 5, 3);
System.out.println(“The result is: ” + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
“`

In this example, we use reflection to create an instance of the `Calculator` class and call its `add()` method with two integer arguments.

Conclusion

In this article, we discussed different methods to call a method from a different class in Java. By using inheritance, interfaces, and reflection, you can achieve this goal efficiently. Each technique has its own advantages and use cases, so choose the one that best suits your requirements.

You may also like