wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what's the difference between Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what Watch "what New topic
Author

what's the difference between "override" and "hide"?

Mike Lin
Ranch Hand

Joined: Oct 29, 2002
Posts: 48
Consider the following two classes:

a. Which method overrides a method in the superclass?
b. Which method hides a method in the superclass?
c. What do the other methods do?
(Code formatted for readability)
[ November 12, 2002: Message edited by: Michael Ernest ]

SCJP1.4 <br />Best wishes!<br />中国人!
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Mike
You cannot override static and private methods - you can hide them!
You can read about overriding&hiding in JLS 8.4.8.2 and
j-Think(Overriding,Overloading,...)
Regards,
Jamal Hasanov
www.j-think.com
Mike Lin
Ranch Hand

Joined: Oct 29, 2002
Posts: 48
thanks a lot !
Deepa
Greenhorn

Joined: Aug 22, 2002
Posts: 10
The j-think article says that private methods cannot be overridden.
The code below compiled successfully.
What am I missing here?

Thanks.
Deepa
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You are not overriding the private method. You are hiding it (and you wouldn't be able to call it anyway).


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Well, compiling this code gives you 2 compile time errors, the declarations of methodOne(), methodThree() in class B are illegal

C:\Documents and Settings\Administrator\My Documents\Nov.java:12: methodOne(int) in Nov cannot override methodOne(int) in ClassA; methodOne(int) and methodOne(int) are static
public static void methodOne(int i) {
^
C:\Documents and Settings\Administrator\My Documents\Nov.java:16: methodThree(int) in Nov cannot override methodThree(int) in ClassA; overridden method is static
public void methodThree(int i) {
^
2 errors

Also these 2 classes can't be declared in the same file, since they're both declared public.


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by Alfred Kemety:

Also these 2 classes can't be declared in the same file, since they're both declared public.

I would have said the same thing, by Simon Roberts hit me with a stunner the other day; I'm still not sure I even heard him right, but he insists that nothing in the spec prevents an implementation of the compiler from allowing two public classes in the same file.
This is something I have been repeating the opposite of for years, so I have no idea what Simon meant unless I misunderstood him, but when things cool down for him I'll inquire. Meantime, I'm in search of a compiler that does in fact allow two public classes in one text file.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
hmmmm, well, you can say there is no spec that says 2 classes can't be declared public in the same file, since you can declare a public nested/inner class and a public top level class in the same file of course, but the spec say that a top level public class should be declared in a file with the same name with an extension java so as to be a legal source file.
[ November 12, 2002: Message edited by: Alfred Kemety ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what's the difference between "override" and "hide"?