File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion!

 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 289
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only (1) is correct.
Static methods cannot be overridden, can only be
hidden. This is clearly described by JLS.
--javamiller
 
Manish Kumar
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all,
Somebody please do reply.
vivek
 
javamiller
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can override static method.
1. True
2. False
Answer is definitely False. Only instance method can be
overriden.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
For me also answer 1,2 looks correct.
please correct me if I am wrong
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic