SCJA | SCJP6 | OCE SQL | OCA DBA 10g
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
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.
SCJP6.0,My blog Ranchers from Delhi
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.
SCJP6.0,My blog Ranchers from Delhi
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.......
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
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.
SCJP6.0,My blog Ranchers from Delhi
phil sohan wrote:i think when the argument type are different then it will be overloading............
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.
SCJP6.0,My blog Ranchers from Delhi
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.
No, It's overriding, not overloading
SCJP6.0,My blog Ranchers from Delhi
phil sohan wrote:
No, It's overriding, not overloading
your this statement confused me............
phil sohan wrote:
No, It's overriding, not overloading
your this statement confused me............
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
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.
SCJA | SCJP6 | OCE SQL | OCA DBA 10g
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 {}
Thanks & Regards
Sumit Kothalikar
sumit kothalikar wrote:
I think Abimaran wants to say that overloading takes place in the same class
while overriding is in different class
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
SCJP6.0,My blog Ranchers from Delhi
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |