File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion!" Watch "Confusion!" New topic
Author

Confusion!

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
This question is from Marcus Mock exam # 3
Which of the following statements are true?

1) static methods do not have access to the implicit variable called this
2) a static method may not be overriden
3) a static method may not be overriden to be non-static
4) a static method may not be overloaded
Answer given is 1,3.
I do agree with answer. but my question is little bit related to the concept of overriding.
As we all know that we can't override the static method( feel free to creect me?) but we can hide the method of base
class by providing same static method in child class. still method of base class is not overriden.
Than why statement a static method may not be overriden to be non-static is correct. because we all know that we can't override static method.

please expalin the conept and right terminology!
Your effort and time would be appreciated!
vivek
Sharat M
Greenhorn

Joined: Jun 26, 2000
Posts: 2
Hi Vivek,
Check this code,
class x
{
public int x;
x()
{x=8;
}

static void dispX()
{
System.out.println("Value of X:");
}

static void dispX(int x)
{
System.out.println("Value"+x);
}
}
class y extends x
{
public int x;
y()
{
x=8;
}
static void dispX() // change static to public, you will get and error.
{
System.out.println("Value of y:");
}

}

public class Testjava
{
public static void main(String args[])
{

x a=new x();

y b=new y();

a.dispX();
b.dispX();

}
}

Therefore , when he says nonstatic, when a static method is over-ridden, the access modifier should be static and not public or private or protected.
I hope this makes it clear for you.
Sharat.


The small world of Java
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi Sharat and all ,
Please try to understand what am i trying to ask. i know if u are trying to override/Hide(???) a static method of base class the new method in child must be static.
One more thing what would u answer the following question

Can we override a static method?
1. Yes
2. No

If u answer No then what about the answer no 3 which says
a static method may not be overriden to be non-static(so this statement wants to say that we can override the static method but it must be static. am i correct?)
If u answer Yes. is it real override? does it follow the concept of Polymorphisim? because i think the concept of overiding provide Polymorphisim.

Java expert please put some light here!
vivek
[This message has been edited by Vivek Shrivastava (edited July 04, 2000).]
[This message has been edited by Vivek Shrivastava (edited July 04, 2000).]
Manish Kumar
Greenhorn

Joined: Jun 27, 2000
Posts: 6
Hi Vivek,
Late-binding of the method is necessary for the polymorphism, but the static methods are bounded by the compiler (early-binding). This shows that you can not use the concept of polymorphism in the case of static methods. But yes, it can be overriden 'coz overriding and polymorphism are not the same thing.

------------------
bante
Greenhorn

Joined: Jun 19, 2000
Posts: 11
How about this:
The base class has the static method
public static void main(String [] args).
and the child class has the same method:
public static void main(String [] args)
How do you explain?
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Vivek,
I think I do share with you the "confusion".I think that the concept of method overriding becomes wishy washy when applied to static methods.In view of the compiler, overriding is when a derived class has a method with exactly the same signature as the superclass's method.Thus if base class has static method method1(), then if in derived class you have method1(), the compiler sees it as an override.When this is done for static methods, it will enforce that method1() in derived class be static as well, for obvious reasons as well.But from an OO point of view, I tend to think that this is really not an override at all, as you very well discuss above.Normally within an overriding method you want to be able to call super.method1() for example to invoke the original version of the method, which you can not do in a "static overriding method".

Herbert
javamiller
Greenhorn

Joined: Jun 24, 2000
Posts: 14
Only (1) is correct.
Static methods cannot be overridden, can only be
hidden. This is clearly described by JLS.
--javamiller
Manish Kumar
Greenhorn

Joined: Jun 27, 2000
Posts: 6

I do not agree with Herbert's argument: "Normally within an overriding method you want to be able to call super.method1() "
The exact definition of Method Overriding is (as given in "The complete reference Java2 by Patrick Naughton):
"In a class hierarchy, when a method in a subclass has the same name and typ signature as a method in the superclass".
This difination doesn't mean that a overriding method should be able to call the super.method1()
But still you can call the original method from the overriding method by
BaseClassName.methodName()
javamiller
Greenhorn

Joined: Jun 24, 2000
Posts: 14
(3) is also correct. Because "a static method may not be overriden to be non-static" does not necessary means
"a static method may be overriden".
So the answer is 1, 3.
--javamiller
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi Herbert,
I do agree with u. but u didn't answer my question.
What would be the most appropriate answer as per exam point of view for the following question.
We can override static method.
1. True
2. False

Please do reply!
regards
vivek
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hello to all,
Somebody please do reply.
vivek
javamiller
Greenhorn

Joined: Jun 24, 2000
Posts: 14
We can override static method.
1. True
2. False
Answer is definitely False. Only instance method can be
overriden.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Manish- well the offical definition of overriding for Java comes not from Patrick Naughton, but from the Java Language Specification:
<blockquote>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.</blockquote>
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?

"I'm not back." - Bill Harding, Twister
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
Now everybody here seems to agree on one point that we can't override the static method in real world and according to JLS. But for the exam point of view override or hide is same.
So for the exam point of view the answer to marcus's question is 1 and 3. but if we go with JLS then answer is 1 and 2.
Did somebody say something?

I would love to hear if someone is not agree with my conclusion.
Thanks for all.

vivek
[This message has been edited by Vivek Shrivastava (edited July 04, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
For me also answer 1,2 looks correct.
please correct me if I am wrong
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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

SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion!