Open In App

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

Last Updated : 10 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Example 1: In this example, lets visualize a car. A car has to start before it is driven. Therefore, a car has a start function which starts the car. In order for a car to start, its engine has to start first and the remaining other entities start thereby finally making the car ready to run. Therefore, this carStart() function need to have a signature engineStart() which starts the engine. That is:




    // Java program to visualize the Car
      
    // A class Car
    public class CarFunc {
      
        // A method engineStart() which
        // simply starts the engine
        public static void engineStart()
        {
            System.out.println("Engine Started!");
        }
      
        // A method carStart() which starts
        // the engine and other entities
        // required
        public static void carStart()
        {
            // Calling the function
            // engineStart() inside the
            // definition of function
            // carStart()
            engineStart();
      
            // Definitions of starting
            // various other entities
            // can be defined here
      
            // Finally, the car is
            // started
            System.out.println("Car Started!");
        }
      
        public static void main(String[] args)
        {
      
            carStart();
      
            // When the car is started,
            // we are ready to drive
            System.out.println("Let's Drive!");
        }
    }

    
    

    Output:

    Engine Started!
    Car Started!
    Let's Drive!
    
  • Example 2: In this example, lets consider a similar example where the entities are accelerator and increase speed. In order to increase the speed of the vehicle, the accelerator is pressed. Once the accelerator is pressed, the speed is increased until it doesn’t reach the speed limit. This is implemented as follows:




    // A carSpeed class which manages
    // the speed of the class
    public class CarSpeedFunc {
      
        // Speed is a global variable. If
        // the speed is changed at any
        // part of the car, then the overall
        // speed of the car automatically
        // changes
        static int speed = 0;
      
        // Function to increase the speed
        public static void increaseSpeed()
        {
            speed = speed + 50;
            System.out.println(
                "Speed increased by 50 km/hr");
      
            // main() called inside this function
            main(new String[0]);
        }
      
        // Method which performs the increase
        // speed function when the accelerator
        // is pressed
        public static void pressAccelerator()
        {
            System.out.println(
                "Accelerator pressed "
                + "to increase speed");
            increaseSpeed();
        }
      
        // Driver code
        public static void main(String[] args)
        {
            System.out.println(
                "Driving with speed = "
                + speed
                + "km/hr");
      
            if (speed < 200) {
                pressAccelerator();
            }
            else {
                System.out.println(
                    "Speed Limit Reached! "
                    + "Drive Slow. Stay safe.");
            }
        }
    }

    
    

    Output:

    Driving with speed = 0km/hr
    Accelerator pressed to increase speed
    Speed increased by 50 km/hr
    Driving with speed = 50km/hr
    Accelerator pressed to increase speed
    Speed increased by 50 km/hr
    Driving with speed = 100km/hr
    Accelerator pressed to increase speed
    Speed increased by 50 km/hr
    Driving with speed = 150km/hr
    Accelerator pressed to increase speed
    Speed increased by 50 km/hr
    Driving with speed = 200km/hr
    Speed Limit Reached! Drive Slow. Stay safe.
    

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads