• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloading final static field  RSS feed

 
Andy McBee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the sample classes, describing problem's structure:




What i need is exactly the following: to create class B in such a way, that it extends A (so that i don't need to copy all the functionality from A), but it's instance uses another value of final static field (i.e. B.getInstance().printS()).
The ways i tried to do it:
- redefining the field itself - doesn't work, 'cause in this case it's not overloading, but hiding
- redefining getter - also doesn't

Does someone have any ideas?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont understand . and welcome to javaranch
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give class B its own static instance field and getInstance() method:
I kept the reference type of instance as A, but you can change it to B if you want.
 
Andy McBee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it works.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please make sure you use the teminology the right way:

Fields cannot be overloaded, only methods can be overloaded. Overloading a method means that you create another method with the same name as an existing method, but with different argument types.

You probably meant overriding instead of overloading.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!