Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot !
 
Deepa
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not overriding the private method. You are hiding it (and you wouldn't be able to call it anyway).
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic