• 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

overriding + overloading

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
Perhaps i'm wrong in telling this, but I did'nt see any example of overriding + overloading process in Dan's mock exam.
Perhaps it may be useful to have a look at a case based itself on Dan's mock example:
Overloading:

outputs : ABC
Explanation:

Class A has only one implementation of method m1. Class B overloads method m1 with a second implementation. Class C overloads method m1 with a third implementation. Reference c2 refers to an object of type A so only the A.m1 implementation is accessible.


Overloading + overriding:
Modification of the precedent code:

Output: CCA
Explanation: c1.m1(c1) is easy to understand.
What about c2.m1(c1)?
Like above, c2 refers to an object of type A. So, c2.m1() invokes A.m1() method's signature and return type at the compile time. Compiler looks for a method compatible with argument's type = C.
void m1(A a) {System.out.print("A");} is successfull.
But since c2 refers to an object of type C at runtime, and since this class overrides m1 from class A, this is this method that is finally invoked: so outputs is CA.
Hope this can help,
Cyril.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, thanks for the example. I would never have realized such an occurance could happen. I had to compile and see it to believe it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic