aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method overloading issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method overloading issues" Watch "method overloading issues" New topic
Author

method overloading issues

hansika motwani
Ranch Hand

Joined: Jul 22, 2008
Posts: 44


class er
{
public static void main(String [] args)
{
b s = new b();
s.display(9,2);
}
}[/code]
1)is this method overloading ? There is a statement in java like method overloading has nothing to do with inheritance and polymorphism then why is this working here ?

2)
can we say the method overloading as compile time polymorphism (static binding)and method overriding as run time polymorphism(late binding)?

3) can we use method overloading even in inheritance then?

There is a rule in overriding that arguments must be the same and return type must be compatible .
RULE 1-->whatever the superclass takes as an argument the subclass overriding the method must use the same argument
RULE 2 -- > Whatever the superclass declares as return type .the overriding method must declare either the same type or a subclass type .remember a subclass object is guaranteed to be able to do anything its superclass declares so its safe to return a subclass where the superclass is expected

4) return type must be compatible ? what does this mean ? is compatible = same ?

In rule 1 does same argument means same type ? or same variable name?

5) to illustrate rule 2 i wrote one program like this

here in subclass i have overridden the method by using same argument type but with different return type which subclass intends ? iam getting compile time error but rule 2 says (this only) as per my view )...Correct me if iam wrong ? rule 2 says remember a subclass object is guaranteed to be able to do anything its superclass declares so its safe to return a subclass where the superclass is expected

6)

here i cant override a public method and make it private .Actually the compiler thinks at compile time that it is a public method ..if suddenly at run time the jvm slammed the door shut because the overriding version called at run time is private . then why am i getting compile time error rather than run time error because the compiler should'nt bother the jvm has to bother as i suppose ?'

please let me know how can i know these issues like compiler considers some issues and jvm bothers about few ..how would i know that ? Does it come by practise or is there any way to know about that

7) During method overloading you can vary the access levels in any direction ..what does this mean ?
8) During method overloading if only the return type is different its not a valid overload- the compiler will assume you are trying to override the method and even that wont be legal unless the return type is a subtype of the return type declared in a super class . what does this mean ?
does that mean we can put any return type in our sublass to make it a legal ?
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Hi Hansika,
Try to keep your post as short as possible, because seeing such a long post makes people run away...........
hansika motwani wrote:
1)is this method overloading ? There is a statement in java like method overloading has nothing to do with inheritance and polymorphism then why is this working here ?

Yes, this is method overloading.
2) can we say the method overloading as compile time polymorphism (static binding)and method overriding as run time polymorphism(late binding)?

Yes, but don't know whether overloading can be polymorphism, but yes its decided at compile time.
3) can we use method overloading even in inheritance then?

Yes. We can use method overloading even when inheritance is going on.
But you cannot call the overloaded method using superclass reference.
In this case, the original display method is inherited using inheritance, and you declare another display method which is overloading it.
So you cannot call the overloaded method display with a super class reference.
4) return type must be compatible ? what does this mean ? is compatible = same ?
In rule 1 does same argument means same type ? or same variable name?

Compatible is not always same.
Compatible means the return type of the overriding class should fulfill IS-A relationship for the return type of the overridden class,
which is what rule 2 says.
In rule 1 same argument means same type.

here in subclass i have overridden the method by using same argument type but with different return type which subclass intends ? iam getting compile time error but rule 2 says (this only) as per my view )...Correct me if iam wrong ? rule 2 says remember a subclass object is guaranteed to be able to do anything its superclass declares so its safe to return a subclass where the superclass is expected

Here the return type is different, but it is not compatible. void does not fulfill IS-A with int. so void IS-NOT int.

here i cant override a public method and make it private .Actually the compiler thinks at compile time that it is a public method ..if suddenly at run time the jvm slammed the door shut because the overriding version called at run time is private . then why am i getting compile time error rather than run time error because the compiler should'nt bother the jvm has to bother as i suppose ?'

In rules for overriding, you cannot make your overriding method less visible, means public cannot be changed to private, protected or even default.
If it is declared private, then in the sub class you can make the method public.You can increase the visibility not decrease it.

please let me know how can i know these issues like compiler considers some issues and jvm bothers about few ..how would i know that ? Does it come by practise or is there any way to know about that

Practice, Study. Then Practice, then study. Practice again, study again................ :lol:

7) During method overloading you can vary the access levels in any direction ..what does this mean ?

This means that during overloading, do whatever you want, means change access levels to whatever you wish.

8) During method overloading if only the return type is different its not a valid overload- the compiler will assume you are trying to override the method and even that wont be legal unless the return type is a subtype of the return type declared in a super class . what does this mean ?
does that mean we can put any return type in our sublass to make it a legal ?

No. I guess the above explains.....


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
1)is this method overloading ? There is a statement in java like method overloading has nothing to do with inheritance and polymorphism then why is this working here ?


Yes this is overloading, overloading happens with in class, while overriding, polymorphism happens across the boundary of class.

here class b has two display methods, one that it inherits from class a, and second it defines itself. So you can see oveloading is happening here with in boundary of class b.

2) can we say the method overloading as compile time polymorphism (static binding)and method overriding as run time polymorphism(late binding)?


For overloading, compiler is able to decide to which overloaded method it has to bind the calling method. No need of late binding.
Late binding is only for overriding.

4) return type must be compatible ? what does this mean ? is compatible = same ?

In rule 1 does same argument means same type ? or same variable name?


Return type compatible means: If superclass method is returning Object, then subclass methods can return Object or subtype of object. Like Number, Integer. This is called covariant return type.
Try to understand this code:



In rule 1 same argument means same type.

5) here in subclass i have overridden the method by using same argument type but with different return type which subclass intends ? iam getting compile time error but rule 2 says (this only) as per my view )...Correct me if iam wrong ? rule 2 says remember a subclass object is guaranteed to be able to do anything its superclass declares so its safe to return a subclass where the superclass is expected


Here you are neither overriding not overloading, in class a display() is return int, while in class b display() is returning void, and void is not a subclass of int. If your class a display() returns Number, and class b display() returns Number, Integer, Float, Double whatever that has extends relationship with Number, then it is valid overriding.


6)
here i cant override a public method and make it private .Actually the compiler thinks at compile time that it is a public method ..if suddenly at run time the jvm slammed the door shut because the overriding version called at run time is private . then why am i getting compile time error rather than run time error because the compiler should'nt bother the jvm has to bother as i suppose ?'

please let me know how can i know these issues like compiler considers some issues and jvm bothers about few ..how would i know that ? Does it come by practise or is there any way to know about that


This is compile time issue, if you have done everything right by syntactically, then only compiler let you go to jvm. Here it is wrong syntax to restrict access modifier of overriden method. These knowledge will come to you by practice and reading only.

7) During method overloading you can vary the access levels in any direction ..what does this mean ?

Means you can change access modifiers like public, private, protected
1) compulsory: You must change argument types.
2) optional: you can change return type also.
3) optional: you can change access modifier of the function


8) During method overloading if only the return type is different its not a valid overload- the compiler will assume you are trying to override the method and even that wont be legal unless the return type is a subtype of the return type declared in a super class . what does this mean ?
does that mean we can put any return type in our sublass to make it a legal ?


I think you can guess its answer now.




SCJP 6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Good Job Sachin, really answering long question takes too much time.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Punit Singh wrote:Good Job Sachin, really answering long question takes too much time.


Really......... :lol:
Was just thinking how long it took to question....... leave the time thinking about it :lol:
Hope it is clear to her now ......
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ya, it seems she is working very seriously.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method overloading issues