Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  About a static superclass and overridden static methods  (Read 1194 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2012-07-22 15:57:18 »

Normally, I like how interfaces work and all, but sometimes I want to use them without making an instance, because its not necessary. Would it be possible to override a static method, but call the method using the superclass?

Like, could I make an object that takes a class as a parameter, only to make a static call on that class (which then might be subclassed/overridden).

This would just be to avoid creating instances when it's not necessary.

I can elaborate on the idea, if necessary.

Cheers guys.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-07-22 16:31:22 »

Static methods cannot be overridden.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2012-07-22 16:32:45 »

Static methods cannot be overridden.

Oh well. I'll use an instance.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2012-07-22 18:10:20 »

What do you really what to do?
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2012-07-22 19:48:46 »

I have a few methods that might as well be static, but theyre overridden. I thought it would be easier to reference the class and use the static method, instead of having a ton of the same objects.

Offline Roquen
« Reply #5 - Posted 2012-07-22 20:41:26 »

If you have a solution then ignore me..but I'm still not understanding what you're saying.  By "might as well be static" you must mean that it doesn't need a access state data of an instance, but beyond that I'm lost.  Can you give a concrete example?
Offline jonjava
« Reply #6 - Posted 2012-07-22 20:56:26 »

You should only ever override methods if you want to add/change something to/in them. To me it looks like you're overriding methods because you want to use them in other objects?

If you want to access the methods from other objects you should either 1) make the class that has them static 2) make the methods static 3) give a reference of that object to the objects that need to use the methods.

#1 and #2 are for lazy people (me). (Try to make as little static classes as  possible).


#3 is true clean good OOP design. I.e:
1  
2  
3  
UtilityObj util = new UtilityObject();

LittleObject lilObj = new LittleObject( util ); // pass the lilObj a reference to your utility object so that it can access the "util" objects public method


E.g:
1  
2  
3  
4  
5  
// LittleObject's constructor
public lilObj( UtilityObject ob ) {
   this.ob = ob; // save the reference for use in other methods of this class.
  myName = ob.getRandomName();
}


There's also another option #4 where you create a non-static class (because you want to avoid creating static classes) but you give the class a static method for accessing the objects reference. (My description is bad, search google for "Singleton" for more detail).

#4 e.g:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class MySingleton {

   private MySingleton mySelf = null;

   private MySingleton() {
      // Make constructor private =
     //    nobody else can create a new MySingleton but itself
  }

   public static MySingleton get() {
      if (mySelf == null) {
         mySelf = new MySingleton();
      }
      return mySelf; // So that other classes can get a reference to this object
  }
}

Offline davedes
« Reply #7 - Posted 2012-07-22 20:59:38 »

Why are you using static in the first place? It sounds like you should be using instance methods.

Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #8 - Posted 2012-07-22 21:37:39 »

It's probably not worth going into the finer points of static method hiding, as it's almost certainly not what you want to be do. (there are situations it's useful, but they're extremely specialized)

As others have suggested, it's much easier to give a 'right' answer if we know precisely what it is you are trying to achieve.
Show us a code example.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #9 - Posted 2012-07-23 11:19:47 »

First of all, thanks for reading and answering everyone!  Smiley For that I am grateful.

You should only ever override methods if you want to add/change something to/in them. To me it looks like you're overriding methods because you want to use them in other objects?

I am aware how to override things, and when it should be done. I also know what design patterns I could apply to make it work like intended. What I tried was something very specific, but it would only work if I could override methods I could access in a static context. I can't do that, though.

Why are you using static in the first place? It sounds like you should be using instance methods.

I could, but that's not what I wanted to do.

I think there is a miscommunication somewhere. I might have explained myself poorly. It doesn't matter though, because I've solved my problem in a different manner. Enough rambling Cheesy

Please avoid replying to this thread anymore. I've found out what I wanted, and I don't think it would be beneficial to keep this debate going. I could explain exactly what I wanted to do, but that would take too long, and I doubt there will be any input of usable substance - at least for me.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (72 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (43 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!