Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding/overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding/overloading" Watch "Overriding/overloading" New topic
Author

Overriding/overloading

Kamila Jolan
Greenhorn

Joined: Jul 12, 2010
Posts: 25
Hi,

For this Question, can anybody help me getting why the result is "parent". I thought this code will not compile because the method from the Child class will be invoked and it takes Child object as a parameter but the Parent reference is passed into it. I know that the object Child is created but still the Parent reference is passed into that method.



Any help will be appreciated.
Rene Argento
Greenhorn

Joined: Jun 09, 2010
Posts: 10
Remember that since you have a different parameter in your show() method in the Child's class you are overloading the show() method in Parent's class and not overriding it. So when you call the method and passes a parent reference, the method show() defined in the Parent class, that Child inherited, will be executed.


SCJA | SCJP6 | OCE SQL | OCA DBA 10g
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

When you invoke a method on the basis of parameter type, the parameter object's declaration type will decide, which method to invoked. For example, in your case,

When you pass the p object to the methods, which have both Parent and Child as arguments, the method with the Parent will be invoked. No virtual method invocation is here. Only the reference type will decide.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Kamila Jolan wrote:Hi,

For this Question, can anybody help me getting why the result is "parent". I thought this code will not compile because the method from the Child class will be invoked and it takes Child object as a parameter but the Parent reference is passed into it. I know that the object Child is created but still the Parent reference is passed into that method.



Any help will be appreciated.



As the parameter of the two methods are different ..........so this is the case of mehod overloading......not the overriding.......
so the refernc type of the object will decide which one to invoke.......


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Kamila Jolan wrote:Hi,

For this Question, can anybody help me getting why the result is "parent". I thought this code will not compile because the method from the Child class will be invoked and it takes Child object as a parameter but the Parent reference is passed into it. I know that the object Child is created but still the Parent reference is passed into that method.



Any help will be appreciated.



when the parameter of the method are different.........then the method is overloaded not a overridden.......
so the refernce type of the method will decide which method to invoke.........as the refernce type is child........
child class inherit the parent class method........
void show(Parent parent) {
System.out.println("parent");
}

as when we invoke the method it wil search for matching argument type.........so it will find the Parent identical with parent........

that why Parent class inherit in child class method will run.............
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

phil sohan wrote:
As the parameter of the two methods are different ..........so this is the case of mehod overloading......not the overriding.......
so the refernc type of the object will decide which one to invoke.......


No, It's overriding, not overloading.

[EDIT] It's correct, it's overloading! check my last post, I made a mistake in the above line! Sorry guys! [EDIT]
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:
phil sohan wrote:
As the parameter of the two methods are different ..........so this is the case of mehod overloading......not the overriding.......
so the refernc type of the object will decide which one to invoke.......


No, It's overriding, not overloading.



i think when the argument type are different then it will be overloading............
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Kamila , Welcome to JavaRanch

phil sohan wrote:i think when the argument type are different then it will be overloading............

exactly! Overloading : method's argument must be different; Overriding : signature[method's name and argument] must be same.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Seetharaman Venkatasamy wrote:Kamila , Welcome to JavaRanch

phil sohan wrote:i think when the argument type are different then it will be overloading............

exactly! Overloading : method's argument must be different; Overriding : signature[method's name and argument] must be same.



it got really confused to my understanding when i see the post which says the method is overridden..........

here is the refernce which say ..........
In order for any particular method to override another correctly :
The return type, method name, type and order of arguments must be identical to those of a method in a parent class.

The accessibility must not be more restrictive than original method.

see reference..........
http://www.akgupta.com/Java/Notes/section6-2.htm


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

phil sohan wrote:here is the refernce which say ..........
In order for any particular method to override another correctly :
The return type, method name, type and order of arguments must be identical to those of a method in a parent class.

The accessibility must not be more restrictive than original method.

yes, you are correct. I mentioned the same. which statement of mine confused you?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051



No, It's overriding, not overloading


your this statement confused me............
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

phil sohan wrote:

No, It's overriding, not overloading


your this statement confused me............


<edit>This is wrong!</edit>

Oops! that is not me
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

phil sohan wrote:

No, It's overriding, not overloading


your this statement confused me............


Check this code snip....
Rene Argento
Greenhorn

Joined: Jun 09, 2010
Posts: 10

In order for any particular method to override another correctly :
The return type, method name, type and order of arguments must be identical to those of a method in a parent class.



The return type can also be a subtype of the overriden method's return type.
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Abimaran wrote:

Check this code snip....
view plaincopy to clipboardprint?

1. class Base {
2. public void method(Vehicle v) {
3. System.out.println("Base - Vehicle");
4. }
5. public void method(Car c) {
6. System.out.println("Base - Car");
7. }
8. }
9.
10. public class Sub extends Base {
11. public void method(Vehicle v) {
12. System.out.println("Sub - Vehicle");
13. }
14. public void method(Car c) {
15. System.out.println("Sub - Car");
16. }
17.
18. public static void main(String[] args) {
19. Base b1 = new Sub();
20. Car c1 = new Car();
21. b.method(c);
22. }
23. }
24.
25. class Vehicle {}
26. class Car extends Vehicle {}


I think Abimaran wants to say that overloading takes place in the same class
while overriding is in different class


Thanks & Regards
Sumit Kothalikar
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

sumit kothalikar wrote:
I think Abimaran wants to say that overloading takes place in the same class
while overriding is in different class

Sorry guys, I little bit confused there. Extremely Sorry. If we change the method signature(Changing the parameter type to its sub type) will makes those methods to overloaded versions. Here the coding?


Thanks for correcting me!
Kamila Jolan
Greenhorn

Joined: Jul 12, 2010
Posts: 25
Thanks very much for all your help and warm welcoming on the forum. Now I got why it is giving such a result.
Thanks once again it was really helpful
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

last thing about overloading and overriding....................

both overloading and overriding can be there in the same class....................
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding/overloading
 
Similar Threads
Static members | Inheritance
Polymorphism ...how to deal with variables
Problem -- Virtual Method Invocation
Inheritance
overridden method