-1

I am trying to work with inner classes and I some questions about them I have a gameObject class as follows

import java.io.*;
public abstract class GameObject implements Serializable {
    protected enum GAME_OBJECT_TYPE {SNARK, HINT, BONUS_ITEM, INVALID};
    protected GAME_OBJECT_TYPE eGameObjectType;
    protected GameObject() {
        eGameObjectType = (GAME_OBJECT_TYPE.INVALID);
        System.out.println("SOMETHING WENT WRONG");
    }
    public class Snark extends GameObject implements Serializable {
        public Snark() {
            eGameObjectType = (GAME_OBJECT_TYPE.SNARK);
            System.out.println("SNARK LIVES HERE");
        }
    }
    public class Hint extends GameObject implements Serializable {
        public Hint() {
            eGameObjectType = (GAME_OBJECT_TYPE.HINT);
            System.out.println("HINT LIVES HERE");
        }
    }
}

With this GameObject class structure I get no errors but the only way I can instantiate a Snark in my GameFrame without errors is

GameObject mySnark = new GameObject.Snark();   // all good
GameObject mySnark = new Snark();   // cannot be resolved

However when I change the gameObject class to

import java.io.*;
public abstract class GameObject implements Serializable {
    protected enum GAME_OBJECT_TYPE {SNARK, HINT, BONUS_ITEM, INVALID};
    protected GAME_OBJECT_TYPE eGameObjectType;
    protected GameObject() {
        eGameObjectType = (GAME_OBJECT_TYPE.INVALID);
        System.out.println("SOMETHING WENT WRONG");
    }
}    
public class Snark extends GameObject implements Serializable {   // The public type Snark must be defined in its own file
    public Snark() {
        eGameObjectType = (GAME_OBJECT_TYPE.SNARK);
        System.out.println("SNARK LIVES HERE");
    }
}    
public class Hint extends GameObject implements Serializable {
    public Hint() {
        eGameObjectType = (GAME_OBJECT_TYPE.HINT);
        System.out.println("HINT LIVES HERE");
    }
}

I can now instantiate a Snark in my GameFrame as

GameObject mySnark = new Snark();   // no errors

I can re-split each object into its own class again but it thought this may be a way to greatly reduce the number of classes that I currently have.

Can someone help me understand what I have got wrong?

Thank you

Walkerbo
  • 545
  • 8
  • 16
  • 1
    what is it that you are trying to accomplish by moving the various classes (snark, etc.) into GameObject? – Richard Chambers May 28 '16 at 00:03
  • 1
    Well, the compiler is right: you have to use a different syntax with those types of inner classes. I think you might be confusing inner classes with inheritance; the two are totally different. – markspace May 28 '16 at 00:06
  • 1
    the compiler is correct, you must fully qualify the inner class when instantiating. – S Meaden May 28 '16 at 00:14
  • Hi all Wow really fast answers. Moving the child classes into the parent class was just cutting down the number of files and as each child has only one attribute I thought that inner classes may be a good way to go. Yes I think that I am confusing the parent child vs inner classes. – Walkerbo May 28 '16 at 00:30

1 Answers1

1

Inner classes are not meant to reduce number of classes in your project (as your question suggests); it's about logically grouping functionality to a certain parent class, most commonly for helper classes specific to the parent class. Inner classes would also typically not extend their containing parent class.

Per your example, there appears to be a need to instantiate objects of type Snark outside of GameObject, which suggests Snark should be an independent class.

More info on inner classes from Oracle's JavaDoc.

CoolBots
  • 4,770
  • 2
  • 16
  • 30
  • Hi CoolBots Thank you for the lightening answer. Yes I think that I still am very shaky on inner classes vs parent child classes. – Walkerbo May 28 '16 at 00:32