| Author |
true or false
|
Dhanashree Mankar
Ranch Hand
Joined: Aug 25, 2003
Posts: 123
|
|
class super{ public void method1(){ System.out.println("super"); } class sub extends super{ public void method1(){ System.out.println("sub"); } public static void main(String args[]){ super a = new sub(); a.method1(); //1 ((sub)a).method1(); //2 } output==> sub sub 1>This means during method overriding method is invoked according to class of current object? 2>If super class doesn't have method1() then line 1 will give compiler error saying method1 is not defined in super.but line 2 will give output sub class of the object is sub then Why line 1 is giving error? 3>Now if method1 is made static in both the classes then line 1 will give output as "super" and line 2 will give output as "sub". Now method1 is redefined in sub class.then why it is not giving error as static method cann't be overriden?
|
 |
jaya S K
Greenhorn
Joined: Aug 18, 2003
Posts: 20
|
|
Hi I am trying to answer tha questions.If i am wrong someone can correct me. <b>class super{ public void method1(){ System.out.println("super"); } class sub extends super{ public void method1(){ System.out.println("sub"); } public static void main(String args[]){ super a = new sub(); a.method1(); //1 ((sub)a).method1(); //2 } output==> sub sub</b> qn 1. ***** This means during method overriding method is invoked according to class of current object? During method overridding,method is invoked according to runtime class of the object. In the code the runtime class of "a" is sub.So method1() of sub is invoked. qn 2. ***** If super class doesn't have method1() then line 1 will give compiler error saying method1 is not defined in super.but line 2 will give output sub.class of the object is sub then Why line 1 is giving error? If superclass does not have method1(),then method1() in subclass becomes the instance method for that class. In this class the reference type of "a" is taken into account. The reference type of "a" is superclass and it has no idea about method1() as method1() is available only in sub class.So compilation error occurs at line 1. qn 3. **** Now if method1 is made static in both the classes then line 1 will give output as "super" and line 2 will give output as "sub".Now method1 is redefined in sub class.then why it is not giving error as static method cann't be overriden? If static method is invoked, only the reference type of the object is considered. When method1() is declared as static in both classes,when it is invoked on a variable with reference type as super then super class method1() is invoked.when it is invoked on a variable with reference type as sub then sub class method1() is invoked. Static methods should not be redefined in subclass to be non static.They can be redefined to be static. Regards Jaya S K
|
 |
Hanna Habashy
Ranch Hand
Joined: Aug 20, 2003
Posts: 532
|
|
hi Dhanashree: Your explenation is correct. in overriding, the compiler look at the referenc variable to make sure that the method is there. The JVM looks at the object type. The program compiled and run. it prints "sup" folowoed by "sub". The only error is that "super" is a reserved JAVA word, and you can't use it as identefire. For the third quistion about <static overriding>. Static methods can be overriden. The overridden mehtod in the subclass will hide the static method of its superclass. I the case of static overriding, the JVM will invoke the static version of the reference type. Hope that will help Hanna
|
SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
|
 |
Jeff Bosch
Ranch Hand
Joined: Jul 30, 2003
Posts: 804
|
|
Static methods can't be overridden! When you use the same signature and return type of a superclass static method in a subclass you are redefining, not overriding, the method. You can test this by changing just the return type and leave the rest of the signature alone. Compile the source code, and check the error message, which will be something like: error, cannot override a static method; return type incorrect Regards, Jeff
|
Give a man a fish, he'll eat for one day. <br />Teach a man to fish, he'll drink all your beer.<br /> <br />Cheers,<br /> <br />Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
|
 |
 |
|
|
subject: true or false
|
|
|