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 3 of 3

Thread: Is it OK to have a superclass just to hold variables common to their subclasses?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is it OK to have a superclass just to hold variables common to their subclasses?

    I have two classes that have no methods:

    Weapon
    ----------
    String name
    int cost
    int requiredStrength
    int requiredDexterity
    int minDamage
    int maxDamage

    Armor
    ---------
    String name
    int cost
    int requiredStrength
    int defenseRating

    First question: is it OK to have a classes like this?
    Second question: the variables 'name', 'requiredStrength', and 'cost' are common to both classes. Does it make sense to factor them out into a superclass, like the one below:

    Equipable
    ----------
    String name
    int cost
    int requiredStrength

    third question: is it good practice to have a superclass just for the sake of factoring out common variables? And if so, is it better in this case to use protected access rather than private?


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

    Default Re: Is it OK to have a superclass just to hold variables common to their subclasses?

    Either one makes sense. But at this point, I'd worry more about what makes sense to YOU, not whether it's the exact correct program setup.

    You might also look into abstract classes and interfaces.

    Or you could have container classes for your statistics, and those classes would all have their own instance of that container class. That way a Weapon or an Armor would HAVE-A statistics Object, which in turn would keep track of the stats of that item.

    But like I said, you should mostly worry about what makes the most sense to you.
    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
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Is it OK to have a superclass just to hold variables common to their subclasses?

    Quote Originally Posted by jrahhali View Post
    First question: is it OK to have a classes like this?
    Yes, they look fine. Nothing weird.

    is it good practice to have a superclass just for the sake of factoring out common variables? And if so, is it better in this case to use protected access rather than private?
    It's not always true, two or three classes may have some identical variables, it's trivial. No need to refactor for a superclass. But it depends on you, the designer of your program.

Similar Threads

  1. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 07:34 PM
  2. Overriding Superclass Static Methods, Using the Superclass Method
    By snowguy13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 21st, 2011, 07:30 AM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. Link From A Driver Class To The Subclasses
    By angels in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 29th, 2011, 07:39 AM
  5. EXAMPLE OF FILTERSTREAMS SUBCLASSES.
    By ordonezc78 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 22nd, 2011, 11:38 PM