• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Difference between Hidden and Overridden

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following on the numerous questions on hidden and overridden methods, please can anyone explain what goes for the SCJP exam. In other words, from the point of view of SCJP,
"Static methods cant be overridden"
Is this statment true or false??
In the real world, as pointed out by someone(I forget whom) in some other thread(uh, I forget which), this is really true since static methods are hidden not overridden
Question is, from point of view of SCJP, what does One
answer if this appears in the SCJP exam?? "True", or "false"??
RCT
------------------
Romba Chinna Thambi
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i feel the answer should be true.
regards
deekasha
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods cant be overridden ..
they can only be hidden ..
bcos overridind is a property of objects ..
while these methods r attached 2 classses rather than objects ..
bye ...
manal
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the ans is false, bcoz static methods can be overridden -only they can't be overridden to be non static. The fact that static method exists for a class rather than for every instance of the class - should not be a constraint for overriding.
Its private methods that can't be overridden - as they are not even visible to the subclass. And ofcourse final methods also can't be overridden.
Please correct me if I'm wrong!
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you define a method in a sub class which has the same signature as a static method in the super class, the compiler enforces the following conditions:
1. The sub class method also has to be static
2. The return type of the sub class method should be the same as that of the super class method
The second condition is similar to one enforced for overriding instance methods. But if you create an instance of the subclass and invoke the static method, the method which gets invoked depends on the reference type. This is shown in the following code:

public class a {
static void staticMethod() {
System.out.println("static method in class a");
}
public static void main(String[] args) {
b b1 = new b();
b1.staticMethod();
a b2 = new b();
b2.staticMethod();
}
}
public class b extends a {
static void staticMethod() {
System.out.println("static method in class b");
}
public static void main(String[] args) {
b b1 = new b();
b1.staticMethod();
a b2 = new b();
b2.staticMethod();
}
}

The output is:

static method in class b
static method in class a

This behavior is entirely different from overriding. Hence, static methods are NOT overridden.
Do you agree?
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that static methods are not overridden in the same sense as non-static methods (ie they are hidden) but does anyone know what the exam would expect? Or does a question like this ever come up?
In reading RHE, they say static methods may not be overridden to be non-static. They do not specifically say static methods may not be overridden. To me this imples that the "hiding" is considered to be a form of overidding.
 
daryl olson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I did what I should have done sooner; I searched this forum for hide and override.
I found an thread on this here:
http://www.javaranch.com/ubb/Forum24/HTML/002192.html
I think that the best responses came from Jim Yingst and Marcus Green. Here's what Jim had to say:
Manish- well the offical definition of overriding for Java comes not from Patrick Naughton, but from the Java Language Specification:
If a class declares an instance method, then the declaration of that method is said to override any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class.
The key part here is the phrase instance method. Static/class methods are covered in the next section, Hiding.
The JLS is very clear - you can't override a static method; you hide it. However, to answer Vivek's question, from an exam point of view it may be best to assume that when they say "override" they really mean "override or hide". I say this only because RHE, who were heavily involved in creating the exam, do not appear to make a distinction between hiding and overriding. There's an old discussion about this here. For what it's worth though, I don't think anyone's ever mentioned seeing a question that depended on this in the real exam (and if someone had seen such a question, I'm pretty sure we would have heard complaints about it here.) So: for the exam, don't worry about it, and for the real world, stick to the JLS definition. OK?
And here is Marcus's response:
From the exam point of view I have read feedback from large numbers of people who have taken the exam nad nobody has reported such a question so far.
So I am going to ammend my mock exams to take this question option out.
Marcus
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Well, static methods r not overridden and i don't understand what u mean by hiding a method. As far as i know varibles r hidden.
Anyway, overridding has a relation with inheritence, but since static methods r class methods they r not inherited and so can't be overridden. And i think they r neither hidden coz u can always access them as Classname.staticmethod(), so how they r hidden i don't know.
Anything wrong?
Thanks
Sanjeet

[This message has been edited by Sanjeet Karamchandani (edited August 19, 2000).]
 
thomas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjeet:
static methods ARE inherited like instance methods. The discussion has been about what happens when a sub class defines a method with the same signature as a static method in the super class.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Daryl, for sharing the wise words of Jim and Marcus.
For further reading pls refer to JLS 8.4.8.5 for an excellent explanation.
A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods.
[This message has been edited by Harry Chawla (edited August 19, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic