• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overriding methods

 
Greenhorn
Posts: 12
1
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I am currently studying Overriding methods using the "Oracle Certified Professional Java SE7 Programmer Exams 1z0-804 and 1z0-805" and I am a bit confused.
I am studying the examples given on pages 70 - 73 of the book and I was wondering if ye guys can help me to understand the information.

The original method is:


The first piece of code:


Which outputs:

p1 equals p2 is false
p1 equals p3 is true



Then the main method is changed to:


Which outputs:

p1 equals p2 is false
p1 equals p3 is false



Could somebody explain to me what is going on? I don't want to be spoonfed, I just cannot wrap my head around this bit.
Why is the method not overridden?
Also why doesn't the second version of main not output the expected output(false, true)?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vivienne Ryan wrote:
Could somebody explain to me what is going on? I don't want to be spoonfed, I just cannot wrap my head around this bit.
Why is the method not overloaded?
Also why doesn't the second version of main not output the expected output(false, true)?



Actually, the two methods are overloaded. What they are not, and what you were expecting, were for them to be overridden. Choosing between overloaded methods is done at compile time, and the compiler does so with the reference types.

Henry
 
Vivienne Ryan
Greenhorn
Posts: 12
1
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Vivienne Ryan wrote:
Could somebody explain to me what is going on? I don't want to be spoonfed, I just cannot wrap my head around this bit.
Why is the method not overloaded?
Also why doesn't the second version of main not output the expected output(false, true)?



Actually, the two methods are overloaded. What they are not, and what you were expecting, were for them to be overridden. Choosing between overloaded methods is done at compile time, and the compiler does so with the reference types.

Henry



Thanks for catching my mistake. I meant to say overridden.
 
Marshal
Posts: 80649
475
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you think you are overriding methods, always use the @Override annotation. You can get all sorts of weird errors if you make that sort of mistake, or tiny spelling errors, as happened to somebody in this thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic