what's the difference between "override" and "hide"?
Mike Lin
Ranch Hand
Joined: Oct 29, 2002
Posts: 48
posted
0
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
posted
0
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
posted
0
thanks a lot !
Deepa
Greenhorn
Joined: Aug 22, 2002
Posts: 10
posted
0
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
posted
0
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
posted
0
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.
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
posted
0
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 ]