How to call a method of different class in Java?

Introduction

In object-oriented programming, methods are a fundamental concept that allows objects to perform specific actions. When working with classes in Java, calling methods of different classes is a common requirement. This article will guide you through the process of calling methods of different classes in Java.

Getting Class Objects

To call a method of a different class, you first need to obtain the class object of the method’s class. Here’s how you can do it:

  • From the Parent Class: If the method is in a parent class, you can call it directly on the parent class object.
  • From the Child Class: If the method is in a child class, you need to cast the child class object to the parent class.
  • Using Reflection: Java provides a way to inspect and invoke methods of objects at runtime, known as reflection. You can use reflection to call methods of different classes.

Methods of Different Classes

Here are some examples of how to call methods of different classes in Java:

Method Calling

Method Calling with Parent Class

public class Parent {
public void method() {
System.out.println("Parent method called");
}
}

public class Child extends Parent {
@Override
public void method() {
System.out.println("Child method called");
}
}

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

In this example, the Child class overrides the method() method of the Parent class and provides a different implementation.

Method Calling with Child Class

public class Parent {
public void method() {
System.out.println("Parent method called");
}
}

public class Child extends Parent {
@Override
public void method() {
System.out.println("Child method called");
}
}

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

In this example, the Child class is the child class, and it inherits the method() method from the Parent class.

Using Reflection

Java’s reflection API allows you to inspect and invoke methods of objects at runtime. Here’s an example of how to call methods of different classes using reflection:

import java.lang.reflect.Method;

public class Parent {
public void method() {
System.out.println("Parent method called");
}
}

public class Child extends Parent {
@Override
public void method() {
System.out.println("Child method called");
}
}

public class Main {
public static void main(String[] args) throws Exception {
Class<?> parentClass = Parent.class;
Class<?> childClass = Child.class;
Method parentMethod = parentClass.getMethod("method");
Method childMethod = childClass.getMethod("method");

parentMethod.invoke(parentClass.newInstance());
childMethod.invoke(childClass.newInstance());
}
}

In this example, we use reflection to get the method object of the Parent and Child classes, and then invoke the method() method using the invoke() method.

Best Practices

  • Follow Interface segregation principle: When designing classes, ensure that they follow the interface segregation principle, which states that a class should have multiple interfaces that are relevant to its behavior.
  • Use generics: Generics allow you to use the same class with different types, which can help avoid type-related issues.
  • Use Dependency injection: Dependency injection allows you to pass dependencies to objects, which can help simplify your code and reduce coupling.

Conclusion

Calling methods of different classes in Java requires some understanding of the object-oriented programming principles and Java’s reflection API. By following best practices and using the right techniques, you can write clean, maintainable, and efficient code. Remember to keep your classes simple, avoid coupling, and use generics and dependency injection as needed.

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