Results 1 to 7 of 7
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    7

    Default Class Extensions

    I have a class called Sprite which extends its several subclasses. Therefore, there are a lot of different Sprite classes, the thing is however, most of those subclasses have unique types of variables which I want to only be included in those particular subclasses, not anywhere else. For instance, I might have a variable measuring distance in one subclass, and in another subclass there might be a height variable inherent. I don't want the first subclass to have both variables, neither the second or the main class. Because before I initialize my subclasses, I need to create the constructors of those subclasses in the main Sprite class first because it doesn't have the unique variables which those classes consist of. How do I prevent that? Now I have to create the unique constuctors and variables for every subclass, when I only want them in their associated classes.

    Thanks in advance!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Class Extensions

    Can you give us a *small* example of what you're talking about? You only need a couple classes with some example fields.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    7

    Default Re: Class Extensions

    This is purely an example, but still features a reminiscent issue. I don't want to have to create those variables from Example2 and Example3 variables in the main Example class, only in the classes where they belong. (Note, in the real thing I have many more variables and it wouldn't be worth implementing the variables of the main class in every subclass and not extend them, because the main class also has important methods that I don't want to redo for every class.)

    Java Code:
    public class Example{
    	private double x, y;
    	
    	public Example(double x, double y){
    		this.x = x;
    		this.y = y;
    	}
    }
    Java Code:
    public class Example2 extends Example{
    	private double potatoes, mushrooms;
    	
    	public Example2(double potatoes, double mushrooms){
    		super.Example(Math.random() * 512, Math.random() * 512);
    		
    		this.potatoes = potatoes;
    		this.mushrooms = mushrooms;
    	}
    }
    Java Code:
    public class Example3 extends Example{
    	private double carrots, eyes;
    	
    	public Example3(double carrots, double eyes){
    		super.Example(Math.random() * 512, Math.random() * 512);
    		
    		this.carrots = carrots;
    		this.eyes = eyes;
    	}
    }

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Class Extensions

    Hi Zelaine,

    Every time I think I'm getting close to understanding your problem, I lose my train of thought. Using your previous example can you please elaborate on your problem.

    There are a couple of things I have spotted in the current code though.

    Firstly, you do not need to call super.Example(). This should just be super() to call the constructor of the super class.

    Secondly, as you randomly generate the parameters passed back to the super constructor you could just create a default constructor in the Example class which generates these values. This in turn would be called automatically when constructing the subclasses eliminating the need for the call to super all together.

    Regards.

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    7

    Default Re: Class Extensions

    The random generation is completely unrelated to the problem, I just wrote whatever I came up with when I was writing the example. I will try to explain this better, just hang on.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Class Extensions

    Quote Originally Posted by Zelaine View Post
    before I initialize my subclasses, I need to create the constructors of those subclasses in the main Sprite class first because it doesn't have the unique variables which those classes consist of. How do I prevent that? Now I have to create the unique constuctors and variables for every subclass, when I only want them in their associated classes.
    Subclasses do not have to have the same information as the parent classes.

    Java Code:
    class Example {
       private int a; // common to all subclasses
       private int b;
       public Example(int a, int b) {
          this.a = a;
          this.b = b;
       }
       public int getA() {
          return a;
       }
       public int getB() {
          return b;
       }
    }
    
    class Ex1 extends Example {
       private int c; // only Ex1 knows about c
       public Ex1(int a, int b, int c) {
          super(a,b);
          this.c = c;
       }
       public int getC() {
          return c;
       }
    }
    
    class Ex2 extends Example {
       private int d; // only Ex2 knows about d
       public Ex2(int a, int b, int d) {
          super(a,b);
          this.d = d;
       }
       public int getD() {
          return d;
       }
    }
    
    class Ex3 extends Example {
       private int e;
       private int f;
       public Ex3 (int a, int b, int e, int f) {
          super(a,b);
          this.e = e;
          this.f = f;
       }
       public int getE() {
          return e;
       }
       
       public int getF() {
          return f;
       }
    }
    All subclasses still have access to a and b thru the parent getters, but not to each others unique values.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    7

    Default Re: Class Extensions

    Thank you so much! I'm amazed of how you could comprehend my diffuse explanation.

Similar Threads

  1. Replies: 10
    Last Post: 03-05-2012, 05:44 AM
  2. java extensions
    By ahmakki in forum New To Java
    Replies: 0
    Last Post: 02-14-2012, 04:59 PM
  3. Running extensions other than .class
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 08:57 AM
  4. How to compare file extensions?
    By Rubin in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 09:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •