Function overloading – Compile time / Runtime ?

Is function overloading Compile time or Runtime? Does JVM has control over which overloaded method needs to be called or it is engraved in the class file itself. Simple question!!Yet often forgotten even by nerd java programmers.

           Try solving the below problem without java.exe 🙂

class SuperClass {}
class SubClass extends SuperClass {}
public class OverloadedTest {
	public static void main(String[] args) {
		SuperClass superClass = new SuperClass();
		SuperClass subClassInstance = new SubClass();
		print(superClass);
		print(subClassInstance);
	}

	/**
	 * @param superClass
	 */
	private static void print(SuperClass superClass) {
		System.out.println("superclass is parameter");

	}
	/**
	 * @param subClass
	 */
	private static void print(SubClass subClass) {
		System.out.println("subclass is parameter");
	}
}


Output: (select below to view the output)

 

            superclass is parameter

           superclass is parameter

Reason behind this is java decides on the method to be called at compile time. That is, the decision is made based on the “type” of the object at compile time rather than waiting for the real object to be created (runtime polymorphism)

Now I hope you will make quick answer to the below

class SubClass extends SuperClass {}
class AppSuperClass {
	/**
	 * @param superClass
	 */
	public void print(SuperClass superClass) {
		System.out.println("AppSuperClass:superclass is parameter");

	}
	/**
	 * @param subClass
	 */
	public void print(SubClass subClass) {
		System.out.println("AppSuperClass:subclass is parameter");

	}
}

class AppSubClass extends AppSuperClass {
	/**
	 * @param superClass
	 */
	public void print(SuperClass superClass) {
		System.out.println("AppSubClass:superclass is parameter");

	}
	/**
	 * @param subClass
	 */
	public void print(SubClass subClass) {
		System.out.println("AppSubClass:subclass is parameter");

	}
}
public class OverloadedTest {
	public static void main(String[] args) {
		AppSuperClass appSuperClass = new AppSuperClass();
		AppSuperClass appSubClass = new AppSubClass();
		SuperClass superClass = new SuperClass();
		SuperClass subClassInstance = new SubClass();
		/*
		 * Making request to print AppSuperClass
		 * 	1. Passing SuperClass instance
		 *  2. Passing SubClass instance (*make note of the type) 🙂
		 */

		appSuperClass.print(superClass);
		appSuperClass.print(subClassInstance);
		/*
		 * Above is repeated with AppSubClass instance
		 */
		appSubClass.print(superClass);
		appSubClass.print(subClassInstance);
	}

}

 

 

Output: (select below to view the output)

           AppSuperClass:superclass is parameter

           AppSuperClass:superclass is parameter

           AppSubClass:superclass is parameter

           AppSubClass:superclass is parameter

Now the real game, without changing the type of any objects in overloaded test I want to get an output like,

               AppSuperClass:superclass is parameter

               AppSuperClass:subclass is parameter

               AppSubClass:superclass is parameter

               AppSubClass:subclass is parameter

To put in simple words, we should be able to make java to dynamically resolve the type of subClassInstance in the above program and work accordingly. Sounds like runtime function overloading!! Put your brains to work, I will be back soon with a solution to above problem!!

Without your comments it’s in-complete!! So kindly spare a minute to put your thoughts 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: