• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

static method can be overridden?

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

<SCJP> says "...Remember that static methods can't be overridden! This doesn't mean they can't be redefined in a subclass, but redefining and overriding aren't the same thing."
But this code confused me !



When I compile the code , the compiler said that "info() in Apple cannot override info() in Fruit; attempting to assign weaker access privileges"
It means static method can be overridden ? But <SCJP> says they cannot be overriden !
Screenshot-47.png
[Thumbnail for Screenshot-47.png]
what the compier saied
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Fruit {
public static void info() {
System.out.println("fruit");
}
}

class Apple extends Fruit {

}

Here class level method info is extended in Apple, now if you will call Apple.info() this will five output as Fruit.

This means you wont achieve the intended purpose of overriding, i.e. calling a method depending upon its object at run time, so overriding has no effect but you have that method extended and access to code will be resolved at compile time you will get error if you happen to violate the overriding rules.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just because you got compiler error that don't assign weaker privileges does not prove that static methods can be overridden. for a moment ignore the error. or better assing public instead of protected. then create a new class and in the main method do this



it will print fruit. had there been overriding it would have print apple. overriding happens at runtime. it is late-binding. which method to called is decided at runtime. but here the call is decided on the basis of TYPE of f, which is Fruit. so no overriding.

 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic