Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Overriding Superclass Static Methods, Using the Superclass Method

  1. #1
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Overriding Superclass Static Methods, Using the Superclass Method

    I recently ran into a problem of overriding a superclass's static method in a subclass. I wanted to use the superclass's static method in the subclass's static method (which had the same name as the superclass's method) to, in a way, extend the superclass method.

    Here's a short unrelated example to give an idea:
    public class Foot
    {
     
       public static String getSentence()
       {
     
          return "This is a foot.";
     
       }
    }
    public class Toe extends Foot
    {
     
       public static String getSentence()
       {
     
          return super.getSentence() + " And this foot has a toe.";
     
       }
    }

    However, I got two errors when trying a similar feat:
    1) non-static "super" cannot be reference from static context, and
    2) accessing static field

    Perhaps my idea of inheritance is wrong; or maybe instead of "super.getSentence()" I should use "Foot.getSentence()"?

    Keep in mind that example is much more simple and watered down than my actual program. Any help would be appreciated!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    You can't override a static method. You can only hide it.

    Quote Originally Posted by snowguy13 View Post
    Perhaps my idea of inheritance is wrong; or maybe instead of "super.getSentence()" I should use "Foot.getSentence()"?
    Bingo bango. More generally, you should only refer to a static method using its class, not a reference.

    Recommended reading: Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    Recommended reading: Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    Hahahahaha, that is very ironic; I was just reading that page!

    But also, what I'm afraid is that if I refer to the superclass's method, the changes caused by the method will be loaded into the superclass's fields, not the subclasses.

    I should just be more forthright: here is the code.

    This is the superclass. The problem I'm encountering is initializing towers' base stats (which, obviously need to be different for each tower).
    public class Tower
    {
     
       // ... other methods and fields ...
     
       public static void initializeBase(String name, ElementComposition comp, int[] levelReqs, 
               int cost, int sell, int constructionSpeed, int fireRate, int damage, int range, int bulletSpeed)
       {
     
          towerName = name;
          eComp = new ElementComposition(comp.getComposition());
          levelUpReqs = levelReqs;
          baseCost = cost;
          baseSell = sell;
          baseConstructionSpeed = constructionSpeed;
          baseFireRate = fireRate;
          baseDamage = damage;
          baseRange = range;
          baseBulletSpeed = bulletSpeed;
     
       }
     
       // ... other methods and fields ...
     
    }

    And here's the subclass, which needs to use the superclass's method.
    public class FlareTower extends Tower
    {
     
       public static void initializeBase()
       {
     
          Tower.initializeBase(TowerConstants.FLARE_TOWER_NAME, TowerConstants.FLARE_TOWER_ECOMP, TowerConstants.FLARE_TOWER_LEVEL_REQS, 
                  TowerConstants.FLARE_TOWER_BASE_COST, TowerConstants.FLARE_TOWER_BASE_SELL, TowerConstants.FLARE_TOWER_BASE_CONSTRUCTION_SPEED, 
                  TowerConstants.FLARE_TOWER_BASE_FIRE_RATE, TowerConstants.FLARE_TOWER_BASE_DAMAGE, TowerConstants.FLARE_TOWER_BASE_RANGE,
                  TowerConstants.FLARE_TOWER_BASE_BULLET_SPEED);
     
       }
    }

    Will this setup load the specified data into the FlareTower class, NOT the Tower class?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    Here's a question: Why are those methods static in the first place?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    Here's a question: Why are those methods static in the first place?
    Because the methods are intended to load the BASE stats of each type of tower. Each instance of each tower class will have its stats based on the base stats of each tower group. These base stats only need be loaded once, since they are permanent.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    That seems like a strange setup. If you really want them to be static and independent, then each Tower subclass is going to have to have its own set of static base variables. That seems messy to me. Instead, it sounds like you really want some instance variables with default values, which you can then change in each subclass.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    snowguy13 (December 20th, 2011)

  8. #7
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    If you really want them to be static and independent, then each Tower subclass is going to have to have its own set of static base variables.
    Yeah, that's exactly the problem I've been grappling with. Trying to avoid that situation, that is.
    Instead, it sounds like you really want some instance variables with default values, which you can then change in each subclass.
    So you mean do this in the subclasses' constructors? Because if I define default values in the superclass constructor, they will have to be overridden...

    I almost forgot to express my appreciation for your help, so thank you very much!
    Last edited by snowguy13; December 20th, 2011 at 01:32 PM. Reason: Sounding more grateful
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    Quote Originally Posted by snowguy13 View Post
    Yeah, that's exactly the problem I've been grappling with. Trying to avoid that situation, that is.
    It's a good situation to avoid, and I really think it's a symptom of not using static values how they should be used.

    Quote Originally Posted by snowguy13 View Post
    So you mean do this in the subclasses' constructors? Because if I define default values in the superclass constructor, they will have to be overridden...
    No, that's the thing- static members can't be overridden. What I'm talking about is more like this:


    public class Tower{
       int someVariable = TowerConstants.baseSomeVariable;
    }
     
    public class OtherTower extends Tower{
       public OtherTower(){
          someVariable = TowerConstants.baseSomeVariableForOtherTower;
       }
    }

    That's still probably not the best approach. Something like this is probably better:

    public class Tower{
       int someVariable;
     
       public Tower(int someVariable){
          this.someVariable = someVariable;
       }
    }
     
    public class Main{
       public static void main(String... args){
          Tower whiteTower = new Tower(TowerConstants.whiteTowerValue);
          Tower blackTower = new Tower(TowerConstants.blackTowerValue);
       }
    }

    Quote Originally Posted by snowguy13 View Post
    I almost forgot to express my appreciation for your help, so thank you very much!
    You're very welcome. I hope this helped clear some confusion up.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    What I'm talking about is more like this:


    public class Tower{
       int someVariable = TowerConstants.baseSomeVariable;
    }
     
    public class OtherTower extends Tower{
       public OtherTower(){
          someVariable = TowerConstants.baseSomeVariableForOtherTower;
       }
    }

    This is the approach I will most likely take. Each of Tower's subclasses will have its special upgrades and stat boosts, so I want to be able to store the information in different classes, for my own sake almost more than the sake of the program... Perhaps I'm developing (heh, pun) a bad habit...?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  11. #10
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    Ooh! I just had an idea: what if instead of loading all the boosts and upgrades into each tower class specifically, I just put all of the information into a class called "TowerStats" or annex the variables into "TowerConstants"? That way, I could have one constructor for the Tower class that accepts an integer to determine what type of tower is being built. Would that be appropriate, or would it be too clumsy to have so many variables and constants in a single class (the TowerConstants class, it would be)?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  12. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Overriding Superclass Static Methods, Using the Superclass Method

    That is what is called an anti-pattern, and it's generally not recommended (even though Java itself does it in a few cases). I've recommended my approach above, but really, do whatever fits into your head the best. Don't worry so much about best practices and the like. Chances are no matter what you choose, in six months you'll look back at your code an cringe. But you're the one who has to deal with your code, so just go with whatever feels most natural, without worrying too much about what other people would think.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Using an array which is in a superclass
    By razorfever in forum Object Oriented Programming
    Replies: 2
    Last Post: October 29th, 2011, 12:05 PM
  2. Assigning a Value to a Class extending a Superclass
    By ubermoe in forum Object Oriented Programming
    Replies: 9
    Last Post: September 22nd, 2011, 11:17 AM
  3. Problem with subclass and superclass methods
    By Farmer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2011, 08:43 PM
  4. [SOLVED] Invoking a superclass constructor in my subclass
    By kari4848 in forum Object Oriented Programming
    Replies: 5
    Last Post: April 29th, 2011, 01:12 PM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM

Tags for this Thread