• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Co-variant return/argument types...

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm pretty sure that I'm correct in this, but I would like to be positive. As of Java 5, this works:
class A {

A doAStuff() { }
}

class B extends A {
B doAStuff() { }
}

OK, so that's considered overridden. So, if the instance of class B calls doAStuff(), then it will do the overridden version (i.e. B doAStuff())? And also what about arguments? Will it work the same way? Thanks.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi You are correct,
For eg :

/* This will call B doStuff() {} method */

But if you it to following:
A doStuff(A a) { }
and
B doStuff(B b) { }

This will be considered as Overloading, not Overriding.So



/* this will call A doStuff(A a) {} version

I hope this will clear your doubt.
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

you pretty much answered all your questions..

Some differences if you are using polymorphism, or if you access directly the members.

Copy the following code in your editor:


You'll have the following result:


Normal access:
A: doStuff in "A"
A: check var: "A"
B: doStuff in "B"
B: check var: "B"

Polymorphism access:
B: doStuff in "B"
AB: check var: "A"

Casted access:
B: doStuff in "B"
AB(Casted to B): check var: "B"



When using polymorphism, the methods of the child are called, but the members of the parent are used (see "Polymorphism access").

Regards,
Alex
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one please expalin me the difference between overidding and overloading.
Overrridding -- looks for refernce variable type
Overloading-- looks for whose object is created.

Correct me if iam wrong please..


 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alex and other Ranchers,

When using polymorphism, the methods of the child are called, but the members of the parent are used (see "Polymorphism access").



I have tried the example you sent and saw that when using polymorphic method calls, the methods of the subclass "class B" are
called, but the members of the superclass "class A" are called.

This is interesting and I'm asking myself Why is it calling the members of the superclass instead of the subclass as one would normally assume.

The reason I can think of is that when you call a method (polymorphic) on an object, also the implicit parameter

------------------------------------------------
System.out.println("\nPolymorphism access:");
A ab = new B();

ab.doStuff(); // polymorphic method call - passes an implicit parameter
// doStuff() in B called.

------------------------------------------------

(pointing to the SubClass object in the heap memory) is passed which then resolves to the correct method
to be executed through "Dynamic Binding". (i.e. doStuff in class B is called)

------------------------------------------------
class B extends A {
public char var = 'B';
B doStuff() {
...;
}
}

------------------------------------------------

Whereas when you access the public member variable of the Superclass - there is No Dynamic binding mechanism
to resolve the correct member variable to be called.

Can anyone Please correct me if I'm wrong or Confirm if this is what happens?

Regards,
Siphiwe Madi
[ January 22, 2008: Message edited by: Siphiwe Madi ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic