• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding methods

 
Vivienne Ryan
Greenhorn
Posts: 12
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • 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)?
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic