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:
- 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.
- 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:
public class CarFunc {
public static void engineStart()
{
System.out.println( "Engine Started!" );
}
public static void carStart()
{
engineStart();
System.out.println( "Car Started!" );
}
public static void main(String[] args)
{
carStart();
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:
public class CarSpeedFunc {
static int speed = 0 ;
public static void increaseSpeed()
{
speed = speed + 50 ;
System.out.println(
"Speed increased by 50 km/hr" );
main( new String[ 0 ]);
}
public static void pressAccelerator()
{
System.out.println(
"Accelerator pressed "
+ "to increase speed" );
increaseSpeed();
}
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 {
public static class BriefDescription {
void detail()
{
System.out.println(
"Ferrari 812 is "
+ "an awesome car." );
}
}
public static class DetailedDescription
extends BriefDescription {
@Override
void detail()
{
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." );
}
}
public static void main(String[] args)
{
BriefDescription briefDesc
= new BriefDescription();
BriefDescription detailDesc
= new DetailedDescription();
System.out.println(
"Brief detail of Ferrari:" );
briefDesc.detail();
System.out.println(
"Complete detail of Ferrari:" );
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!