Open In App

How to call a method that returns some other method in Java

A method is a collection of statements that perform some specific task and return the result to the caller. A method can also perform some specific task without returning anything. In this article, we will understand how to call a method that returns some other method in Java.

In Java, there are two types of methods. They are:



  1. Static Method: Static methods are the methods in Java that can be called without creating an object of the class. They are referenced by the class name itself or reference to the object of that class.
  2. Instance Method: Instance method are methods which require an object of its class to be created before it can be called. To invoke an instance method, we have to create an object of the class in within which it defined.

Calling a static method that returns some other static method: Since static method(s) are associated to the class in which they reside (i.e.) they can be called even without creating an instance of the class, we can directly define a method which calls the method by calling the definition of the method. Let’s understand how to do this with the following examples:

Calling a static method that returns some other static method: Instance method(s) belong to the Object of the class, not to the class (i.e.) they can be called after creating the Object of the class. An instance method can also be called from another method. But, we need to know the address of the method which we are calling. The address of the current object is stored in the keywords like this and super. Let’s understand this with an example. In this example, the super keyword is used to call the object of the parent class from the child class. That is:




class GFG {
  
    // A nested parent class
    // which has the method
    // detail
    public static class BriefDescription {
        void detail()
        {
            System.out.println(
                "Ferrari 812 is "
                + "an awesome car.");
        }
    }
  
    // A child class extending the
    // parent class
    public static class DetailedDescription
        extends BriefDescription {
  
        // Overriding the parent
        // method
        @Override
        void detail()
        {
            // Using super keyword to call
            // 'detail()' method from the
            // parent class
            // 'BriefDescription'
            super.detail();
            System.out.println(
                "It has a seating "
                + "capacity of 2, "
                + "fuel economy of 7 kmpl "
                + "and comes with a horsepower "
                + "of 588 kW.");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        BriefDescription briefDesc
            = new BriefDescription();
  
        BriefDescription detailDesc
            = new DetailedDescription();
  
        System.out.println(
            "Brief detail of Ferrari:");
  
        // Method from the parent class
        // is invoked
        briefDesc.detail();
  
        System.out.println(
            "Complete detail of Ferrari:");
  
        // Method from both parent class
        // and subclass is invoked.
        detailDesc.detail();
    }
}

Output:
Brief detail of Ferrari:
Ferrari 812 is an awesome car.
Complete detail of Ferrari:
Ferrari 812 is an awesome car.
It has a seating capacity of 2, fuel economy of 7 kmpl and comes with a horsepower of 588 kW.

Article Tags :