Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

R&H Second Edition SampleTestQ#40

 
jigneshk
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
40. Which of the following is/are true?
A. A static method may be overridden by a static method.
B. A static method may be overriden by a non-static method.
C. A non-static method may be overridden by a static method.
D. A non-static method may be overridden by a final non-static method.

My Answers are A ,D
Answers in the Book : D
Reason in book: A&B are false because static methods may not be overriden;
My sample to prove my answer is
import java.io.*;
class Test {
public static void trial() { System.out.println("Class Test");}
}
public class T extends Test
{
public static void trial() { System.out.println("Class T");}
public static void main (String[] args){
T t = new T();
t.trial();
}
}
The above code compiles and runs to print Class T....
Am I misinterpretting the answer?

Jignesh
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You are right.
But i have a question about that?
Can you override public static void main(String[] argv)
method. Why main() method is always static? I think
it should clear our concept about static and non-static.
Thanks,
G. Newaz

 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But your sample code is noting to do with overriden.
overriden is that JVM will according to object type not
object reference type pick right method at running time
(not compiler time), all static method bind at compiler time( so as your sample
code)
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RHE is correct. You cannot override static methods, not with non-static methods and not with static methods.
If you try to override a static method with a non-static method you will get a compile time error.
If you try to override a static method with another static method it will compile and run, but it will not have the polymorphic behaviour that comes with overriding, thus you are hiding, not overriding. For example if you created an object like this:
Parent p = new Child();
Whenever you call non-static method like p.aMethod() it will first look for aMethod() in the Child class and then look in the Parent class if it is not in the Child class, that is overriding behaviour. But if you call a static method like p.aStaticMethod(); then it will only look in the Parent class.
This is an important point to remember.
Bill
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jigneshk ...
Welcome to JavaRanch Please read our Name Policy and re-register with an appropriate name.
Thanks for your co-operation.

------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic