Hi Nisheet, the first one is a little bit easier. Simply D as the constructor of B is invoked first and then the println(). The second one is a very tricky question trying to divert u with the concept of polymorphism which states that methods r invoked according to the nature of the runtime object, not the refernce.So one could find according to plymorphic behavior of n object the answer should be D but the answer is C.. Because polymorphic behavior r shown by objects and only non-static methods r assoicated with objects while static methods r associated with classes. So in ur question the right method chosen by the compiler is at compile time according to the class of the object reference/variable, not the object itself. Hope it helps.... ------------------ Muhammad Ashikuzzaman (Fahim) Sun Certified Programmer for the Java� 2 Platform --When you learn something, learn it by heart!
Ashik Uzzaman Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Q1: D Simple constructor invocation and static memeber printout Q2: C Static methods cant be overriden. So even though polymorphic reference or lookup takes place ie Base b = new Sub() it doesnt matter coz its static . so aMethod() goes to reference (static binding)
I didn't get a chance to run the code in Q2. But I remembered that static methods don't participate in overriding at all. So is it legal to have the same method signature in the sub class? I prefer A choice in Q2. Please correct me if I'm wrong.
Originally posted by Alan Mar: I didn't get a chance to run the code in Q2. But I remembered that static methods don't participate in overriding at all. So is it legal to have the same method signature in the sub class? I prefer A choice in Q2. Please correct me if I'm wrong.
Hi Alan, The code will comiple w/o any error as it is an example of hiding. No overriding here. The correct answer is C. Here is from JLS 220.127.116.11 Hiding (by Class Methods) "If a class declares a static method, then the declaration of that method is said to hide 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. A compile-time error occurs if a static method hides an instance method. " HTH, - Manish
Originally posted by darryl failla: This suggests that there is no TRUE overriding of static methods between classes and subclasses. Is this true?
Hi Darryl - There's no overriding of static methods, period. The confusion that arises sometimes is when a static method, in terms of its scope protection, is equated with a member that is private or final. When a method is declared static in a class, the subclass is not prohibited from writing a method by the same name. This is a subtle distinction, I think, because it's not easy to understand the difference between method overriding and method "re-writing." The latter of course is not an intended design feature; it's just a consequence of the distinction between a member that cannot be overridden because of scope or finality, and something that gives the impression of being modifiable in a subclass. ------------------ Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
Q1) A.Compilation Error if you have a public static main(String arg) in class A u probably want to name this file A.java right? So it will run the main method right? but your also have a public class B in the file. You can't do that. if you have B.java as your file name. There is no output. Cuz there is no main method to be run.
Joined: Jul 20, 2001
Originally posted by FEI NG: Q1) A.Compilation Error if you have a public static main(String arg) in class A u probably want to name this file A.java right? So it will run the main method right? but your also have a public class B in the file. You can't do that. if you have B.java as your file name. There is no output. Cuz there is no main method to be run.
Actually you have a good point But the answer is not correct It will certainly compile and run with D output public class deserves the file name and here it is B.java Ok , now we dont have a main() in class B... but main() is in class A. Since A is B's parent, B inherits it and executes without problem Hope it helps you Ragu
Hi FEI NG: If you have a public class then the compilation unit must be saved as the name of the public class.java. So, in Q1, the file is B.java. It will compile successfully and will give an output because, the main method from class A is inherited down to class B. Therefore, you can compile : javac B.java and you can run: java B your output will be: inside B x value is: 0
Muhammad, The 'class' is really a template for making additional objects (instances) of the same type. Static items are available to all instances of the type but each instance does not have one as part of it. Imagine the way appliances obtain electricty in your home. There are thousands of 'houses' but all of them are serviced by one generator. A generator is not built in every house. The same applies to static variables or methods. If a subclass does not declare a main() method of it's own, it still has access to the main() method declared for it's type or class. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform [This message has been edited by Jane Griscti (edited October 16, 2001).]
Q2:c I meet this before, and someone told me that here is no polymorphic. these code Base b=new Sub(); b.aMethod(5); is just equals to Base.aMethod(5). so the answer is C. becoz they are static member.
Joined: May 08, 2001
Thanks Jane, I understand now that the static members are not inherited but are still accessible. --Farooq