Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

true or false

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I AM MIGHTY! Especially when I hold this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic