How to call method in another class Java?

How to Call a Method in Another Class in Java?

Java is an object-oriented programming language that allows you to create reusable code. Inheritance, Encapsulation, Polymorphism, and Abstraction are four fundamental concepts in Java. The concept of methods is one of the most significant features that Java provides. A method is a block of code that can be called multiple times by different parts of the program. In this article, we will see how to call a method in another class in Java.

Why Call a Method in Another Class?

Before we dive into the solution, let’s understand why you would want to call a method in another class. Here are some reasons:

  • To reuse code: Calling a method in another class is a way to reuse code. You can write a method once and use it multiple times in your program.
  • To promote modularity: Encapsulation is an important concept in OOP. Bundling similar functionality in a single class promotes modularity and makes the code more maintainable.

Ways to Call a Method in Another Class in Java

There are several ways to call a method in another class in Java. Here are a few:

1. Using Objects

In Java, an object is an instance of a class. You can create an object of one class in another class and call the methods of the first class. Here is an example:

Example:

public class Employee {
public void printName() {
System.out.println("John");
}
}

public class Manager {
public static void main(String[] args) {
Employee employee = new Employee();
employee.printName();
}
}

In this example, the Employee class has a printName() method that prints the name "John". The Manager class creates an object of the Employee class and calls the printName() method using the object.

2. Using Static Methods

In Java, a static method can be called without creating an object of the class. Here is an example:

Example:

public class Employee {
public static void printName() {
System.out.println("John");
}
}

public class Manager {
public static void main(String[] args) {
Employee.printName();
}
}

In this example, the Employee class has a static printName() method that prints the name "John". The Manager class calls the printName() method without creating an object of the Employee class.

3. Using Interfaces

In Java, an interface is a contract that defines a set of methods. An interface can be implemented by multiple classes, and you can call methods on an interface. Here is an example:

Example:

public interface Printable {
void printName();
}

public class Employee implements Printable {
public void printName() {
System.out.println("John");
}
}

public class Manager {
public static void main(String[] args) {
Employee employee = new Employee();
employee.printName();
}
}

In this example, the Printable interface defines a printName() method. The Employee class implements the Printable interface and provides the implementation of the printName() method. The Manager class creates an object of the Employee class and calls the printName() method using the object.

Benefits of Calling a Method in Another Class

Calling a method in another class has several benefits:

  • Reusability: You can reuse code by writing a method once and using it multiple times.
  • Modularity: Encapsulation is promoted by bundling similar functionality in a single class.
  • Easier maintenance: With methods, you can maintain and update code more easily.
  • Improved readability: Code becomes more readable when similar functionality is grouped together.

Conclusion

In this article, we have seen how to call a method in another class in Java. We have also discussed the benefits of calling a method in another class. By following the best practices of object-oriented programming, you can write more maintainable and reusable code.

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