• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

programfollow

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
explain the program follow


NOTE (Jesper Young): Please use [/code] with a forward slash /, not [\code] with a backslash \ to end the code block.
[ October 02, 2007: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods cannot be overridden in subclasses. It doesn't behave like polymorphism These methods are resolved at compile time.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it just warns that m2 should be accessed in a static way but dont see a reason why we cant override it. Doesnt throw a compile error as far as I know. Correct me if I am wrong..
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


System.out.println(((Super)t).m1()); //3


Here m1() is overriden in the subclass, so calling to this method depends on the actual object in this case 't' of subclass, But there is a cast to Super, in this case it doesnt matter because JVM already figured which method to call which again depends on the actual object type.


System.out.println(((Super)t).m2()); //4


Here m2() is redifined in subclass, so casting to type Super will work here because subclass doesnt override it. remember static methods are not inherited, hence cannot be overriden
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B, p. 130:
"static members can also be accessed using an instance reference variable, someObj.theStaticMethodName() but that�s just a syntax trick; the static method won�t know anything about the instance referred to by the variable used to invoke the method. The compiler uses the class type of the reference variable to determine which static method to invoke."

So


is using the trick; the following yields the same and is more appropriate:


Matthias
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
execting the above code will get output like

Test1
Super2

could any one explain it ....
 
suresh mulagala
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
((Super)t).m1()
overrides superclass method so prints Test1

((Super)t).m2()
executes the superclass static method and static method is not overridden,
hence prints out Super2
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic