• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding/overloading

 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.......
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


No, It's overriding, not overloading


your this statement confused me............
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
phil sohan wrote:

No, It's overriding, not overloading


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


Check this code snip....
 
Rene Argento
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
last thing about overloading and overriding....................

both overloading and overriding can be there in the same class....................
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic