• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

overriding

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Above code is not compiling,compiler message is

"Multiple markers at this line
- This instance method cannot override the static method from Top
- overrides Top.myTop"

According to my understanding static methods cannot be overrided.Then why this compile time error?
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both the method signatures of Top.myTop() and Down.myTop()methods are same, so compiler is assuming that you are trying to override the static method myTop() of Top class. That causes the compiler error.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!!!
The reason is the same as explained above we cannot override the static methods....and that is what is attempted in your program.You change the method name in Sub class or remove static modifier from method in super class.....i hope it helps.....
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vice-versa is also true.
I mean you cannot override/hide a non-static method to be a static in sub-class and also you cannot override/hide a static method to be a non-static in sub-class.

Murali...
SCJP 5 Aspirant
 
ramesh kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all
 
ramesh kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all
 
Ranch Hand
Posts: 37
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its fine..please find the following code.
class Top {
static void myTop() {
System.out.println("Testing myTop method in Top class");
}}
public class Down extends Top {
static void myTop() { //compile time error here
System.out.println("Testing myTop method in Down class");
}
public static void main(String[] args) {
Top t = new Down();
t.myTop();
}
}


output:Testing myTop method in Top class
please explain why its given this output
 
Ramu Valivarthi
Ranch Hand
Posts: 37
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its fine..please find the following code.
class Top {
static void myTop() {
System.out.println("Testing myTop method in Top class");
}}
public class Down extends Top {
static void myTop() { //compile time error here
System.out.println("Testing myTop method in Down class");
}
public static void main(String[] args) {
Top t = new Down();
t.myTop();
}
}


output:Testing myTop method in Top class
please explain why its given this output
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are bound to the declared reference type and not the run time type of the object. hence the result.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which Static variable/methods and instance variable to be invoked is "always" decided at compile time. Since at compile time no object is created, hence compiler checks the Class type of the reference variable used to invoke the static method/variable & instance variable and not the runtime object it is refering to. Hence you get that o/p. Dynamic Polymorphism only applies to Instance methods..
 
I was her plaything! And so was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic