Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if there is a superclass that has public static method m()...and there is subclass that extends that Superclass and it is legal to declare to private static method m()

i just want to know if this happens because the static method cant be overridden but redefined in the subclass and none of the overriding rules apply to static methods...

please Confirm

 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4587
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raju Champaklal wrote:if there is a superclass that has public static method m()...and there is subclass that extends that Superclass and it is legal to declare to private static method m()

This isn't true. Have a look at OverridingVsHiding FAQ
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please Confirm


Confirm what ?

You can indeed define a method with the same signature, but it would be hidden, not overridden.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i pasted that code in Netbeans..
Netbeans tells warning in the code :


warning message :

m() in And2 cannot oveeride m() in And; attempting to assign weaker access privileges; was public


you tried to override And's method with And2's method..
And2's method has weaker access (private privileges)..

if you tried to replace the private to public, its mean that you "hiding" the And's method :


i've changed the code as below :


the result is :

And2's m method is executed
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m() in And2 cannot oveeride m() in And; attempting to assign weaker access privileges; was public


Since static methods cannot be over-ridden(and no overriding rules are applied to them), why would compiler even complain about it?


Regards,
Chintan.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually static methods cant be overridden but can be redefined...and the rules for redefining are the same as overridden...its just that actually the polymorphism cant be used with static methods...thats all that is
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it..Thanks Raju.

Wow!! I am learning some great new stuff here...not taught in class. Javaranch rocks!!!

Regards,
Chintan.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will learn a lot of things here that are not taught in the class .

You can go through this stuff too for more clarification. Sorry for bumping the post ..
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sudipto.

Regards,
Chintan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic