This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Overriding methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding methods" Watch "Overriding methods" New topic
Author

Overriding methods

Vivienne Ryan
Greenhorn

Joined: Dec 12, 2013
Posts: 12

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
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vivienne Ryan
Greenhorn

Joined: Dec 12, 2013
Posts: 12

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

Joined: Oct 13, 2005
Posts: 37884
    
  22
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overriding methods
 
Similar Threads
help with JFrame program
equals() method and its significance
flicker in my program
Moving multiple ojects in Java2D
Help: Exception occurred during event dispatching