Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Workaround for abstract static members / class types / members pointing to subclasses

    Hello,

    From what I've gathered I can't have something such as abstract static methods or fields in abstract classes in java, how can I achieve the same or similar effect as an abstract static method or field would achieve in java? I know abstract and static contradict each other but what I mean is a field or method that must be initialized as static fields or methods in any and all subclasses of an abstract class.

    Kind Regards,
    Aqualight
    Regards
    augustas656

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Abstract and static don't contradict each other, they have absolutely no relation to each other. Static is completely isolated from any class hierarchy as it ties something directly to one specific class only, so I'm not really sure what you are after here.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Ideally I want to force static final field or method initialization in every subclass of an abstract class. How can I achieve this or something similar to this? I have an abstract class called ColorSpace that is extended by two classes called RGB and HSB, both have final fields for the dimensions such as Red, Green and Blue but also final static fields for the maximum value that these dimensions can be such as 255, 255 and 255 for the RGB. Instead of seperately defining these in every subclass of ColorSpace I require ColorSpace to force initialization of these range values.

    Kind Regards,
    Aqualight
    Regards
    augustas656

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Still pretty vague. Let me try to understand: you want to force that public static final fields exist in every subclass?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Having an RGB static constant in the ColorSpace class makes no sense, when it's the RGB subclass that is the only thing that needs to know it.
    RGB is not HSB, so presumably you would need HSB constants as well.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Quote Originally Posted by gimbal2 View Post
    Still pretty vague. Let me try to understand: you want to force that public static final fields exist in every subclass?
    Yes or something that will achieve similar effect.

    Quote Originally Posted by Tolls View Post
    Having an RGB static constant in the ColorSpace class makes no sense, when it's the RGB subclass that is the only thing that needs to know it.
    RGB is not HSB, so presumably you would need HSB constants as well.
    I don't want static constants in the ColorSpace abstract class, I want them in every subclass of ColorSpace and not optionally. And because HSB would be a subclass of ColorSpace it should also have public static final max ints for the dimensions that are Hue, Saturation and Brightness.

    Kind Regards,
    Aqualight
    Regards
    augustas656

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    How do you expect this to be enforced?
    They're different constants in different classes.

    I'm having trouble seeing what it is you think should be possible.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    The ColorSpace classes are part of a game engine that I am making and I want it to be very flexible where possible but strict where necessary. I can simply write the maximum values as static final fields for the channels of the RGB and HSB colorspaces but there are other colorspaces out there and if someone wanted to add one to this engine I need them to define or initialize these maximum values. This is so because I want to minimise repetition and perform the range checking of the subclasses within the superclass which is the ColorSpace abstract class and the only way I can see this being done is accessing these final static values like they were abstract methods. Is there any workaround for this problem?

    Kind Regards,
    Aqualight
    Regards
    augustas656

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

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    I don't quite understand the nature of the access/change requirements of your constants. Constants should be, well, constant. Also, you can't force an initialization of a final static constant in an abstract class via, say, a constructor (at least I couldn't get it to work). So I came up with this to primarily clarify what you want. And there may still be problems with it.

    Java Code:
    abstract class ColorSpace {
       private static int      HSB_CONSTANT;
       private static int      RGB_CONSTANT;
       private static boolean IS_INITIALIZED = false;
    
       public ColorSpace(int hsb_value, int rgb_value) {
          if (!IS_INITIALIZED) {
             HSB_CONSTANT = hsb_value;
             RGB_CONSTANT = rgb_value;
             IS_INITIALIZED = true;
          }
       }
    
       protected static int getHSB() {
          if (IS_INITIALIZED) {
          return HSB_CONSTANT;
          } else {
             throw new IllegalStateException("HSB not initialized");
          }
       }
    
       protected static int getRGB() {
          if (IS_INITIALIZED) {
          return RGB_CONSTANT;
          } else {
             throw new IllegalStateException("RGB not initialized");
          }
       }
    }
    
    class Foo1 extends ColorSpace {
       Foo1(int hsb, int rgb) {
          super(hsb, rgb);
       }
    }
    
    class Foo2 extends ColorSpace {
       Foo2 (int hsb, int rgb){
          super(hsb, rgb);
       }
    }
    The idea is that the constants must be initialized by an instance of any single subclass instance (which is sort of weird anyway). Once done, they can't be changed via subsequent instantiations. Because getHSB and getRGB can be accessed in as static way from Foo1 or Foo2, the test of initialization needs to be there.

    Note: I am not endorsing this. Just trying to understand your requirements.

    Regards,
    Jim
    Last edited by jim829; 08-10-2015 at 06:53 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    No offense, but that's entirely unlike my problem, am I that hard to understand? XD

    Okay, err, I'll write an example that cannot compile but crudely illustrates my problem:

    Java Code:
    public abstract class Vehicle {
    	public final String brand;
    	public final String name;
    	
    	public abstract void driveTo(String location);
    	public abstract static int getWheels();
    	
    	public Vehicle(String brand, String name) {
    		this.brand = brand;
    		this.name = name;
    	}
    }
    
    public class Car extends Vehicle {
    	public Color glassTint;
    	
    	public Car(String brand, String name, Color glassTint) {
    		super(brand, name);
    		this.glassTint = glassTint;
    	}
    	
    	public void driveTo(String location) {
    		//Whatever...
    	}
    	
    	public static int getWheels() {
    		return 4;
    	}
    }
    
    public class Motorcycle extends Vehicle {
    	public Color handleColor;
    	
    	public Motorcycle(String brand, String name, Color handleColor) {
    		super(brand, name);
    		this.handleColor = handleColor;
    	}
    
    	@Override
    	public void driveTo(String location) {
    		//Whatever..
    	}
    	
    	public static int getWheels() {
    		return 2;
    	}
    }
    Focus is achieving something like a static abstract method to "getWheels()" since all Motorcycles have 2 wheels and all cars have 4 wheels. How can I do something like this or similar to this? Because abstract static doesn't compile and doesn't work in java

    Kind Regards,
    Aqualight
    Regards
    augustas656

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

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Quote Originally Posted by augustas656 View Post
    No offense, but that's entirely unlike my problem, am I that hard to understand?
    No offense taken. I think the problem is with the receiver, not the transmitter.

    But I don't see the advantage of making the getWheels() method static. That information is strictly related to the subclass. So you just make the method in Vehicle abstract and force each sublcass to properly implement it.

    Regards,
    Jim
    Last edited by jim829; 08-10-2015 at 07:09 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Yah but that seems like a poor workaround, I mean if every instance of motorcycle is supposed to have 2 wheels then having a non-static method is just inefficient. I mean in my game I need to access the number of wheels from subclasses of Vehicle in a static manner, I can't do that unless I make an instance whereas I don't need to and it just wastes processing power, I know it's minimal. But it matters, in my original scenario I have things like color effects and images are made of many many pixels of different colors and that little inefficiency add up and does cost me.

    I mean from what I've gathered java lacks this feature and I've encountered this problem many times in many situations and you can find this same question being asked in so many stackoverflow threads. Damn, I consider making my own programming language, but just how absurd is that? XD I mean I don't even know much about hardware and compiling and their link with programming languages.

    Kind Regards,
    Aqualight

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

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Quote Originally Posted by augustas656 View Post
    Yah but that seems like a poor workaround, I mean if every instance of motorcycle is supposed to have 2 wheels then having a non-static method is just inefficient.
    But that is a quite common situation. There are always going to be subclasses whose instances have identical values for some fields. You may have subclasses of motorcycles that have different colors. But if you code based on that your class hierarchy will become unwieldy. There is absolutely nothing wrong with having the same method of different objects return exactly the same value. I would not worry about the performance hit because it is probably negligible. And any potential work around might actually worsen performance.

    Regards,
    Jim
    Last edited by jim829; 08-10-2015 at 08:26 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    No I mean like, let's say I have a method in Car called selectMoreEfficient that takes a Car... cars parameter and returns the most efficient car based on their speeds and weights as an example, such method should always be static... I mean if I don't find a way to do this I am going to have to leave it as non-static, but is java lacking this very useful feature or is there a very good reason for this or am I just misunderstanding something?

    Kind Regards,
    Aqualight

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

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Quote Originally Posted by augustas656 View Post
    No I mean like, let's say I have a method in Car called selectMoreEfficient that takes a Car... cars parameter and returns the most efficient car based on their speeds and weights as an example, such method should always be static... I mean if I don't find a way to do this I am going to have to leave it as non-static, but is java lacking this very useful feature or is there a very good reason for this or am I just misunderstanding something?
    Since each instance of Car is expected to have a different efficiency rating, it makes sense that it be a characteristic that is associated with an instance. Then you have to go thru the supplied list of cars and use some algorithm to compare the cars against each other. But what you might consider is using the parameters available when you instantiate a Car to determine an efficiency rating. Then store that rating in a Car field of appropriate name. You could even store the cars in a sorted list based on that field so you can then simply take the top N efficient cars from the list.

    Static fields are values are for class, not instance, use. Class methods have no object state and are more simply described as functional methods. They apply to all instances of the class, regardless of any instance specific characteristics. A good example of these are in the java.lang.Math class. Static fields are best used as constants applicable to all instances or some value that is usable by all classes. For example, if I want to track the number of objects I have created, I would use a static field, and increment it each time one is created and decrement it when one is garbage collected (or achieved a similar, unusable state).

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

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    The Vehicle example has problems of its own, but might highlight where I have trouble seeing how what you plan on doing helps you. Not all Vehicles have wheels.

    So, for your ColorSpace problem, not all Color systems use integers...or even 3 fields.
    So what are you going to do?

    Have something like this in the abstract class
    Java Code:
    protected static boolean isWithinRange(int maxValue, int suppliedValue) {
        return suppliedValue <= maxValue;
    }
    ?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Quote Originally Posted by augustas656 View Post
    I mean if every instance of motorcycle is supposed to have 2 wheels then having a non-static method is just inefficient.
    I dare you to actually prove that, because this is just a huge assumption on your part at this point.

    Before you go spend time trying to puzzle out how you can actually do that, I recommend you read this and perhaps change your mind:

    The Developer Insight Series, Part 1: Write Dumb Code -- Advice From Four Leading Java Developers
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Thumbs up Re: Workaround for abstract static members / class types / members pointing to subcla

    Tolls (can't reply with quote):

    I have a class called Range which solves the checking problem, my problem doesn't lie with the checking, as for the 3 fields, the ColorSpace constructor accepts an int... dimensions argument which allows subclasses to define any number of fields, I'm using integers because it's just a measure of the dimensions, sure you can get raw values as floats for RGB and HSB but I think its better with a range.

    Quote Originally Posted by gimbal2 View Post
    I dare you to actually prove that, because this is just a huge assumption on your part at this point.
    The idea of abstract / static I have in mind has properties from both keywords but isn't either exactly, the field or method in the abstract class isn't static because it isn't related to the abstract class, only the subclasses and it isn't abstract either because you can't cast a subclass back to the abstract class as it would remove access to the static variable though this virtual variable can be accessed within the abstract class code not outside. This concept isn't possible in java therefore I cannot prove whether it'd be more efficient.

    Edit: I've tested with final static fields seperately defined in the subclasses and abstract methods, both uses give very varying results though my latest test shows that abstract methods were 2% slower in comparison to final static fields, pretty minimal, I know. I've used two subclasses of an abstract class and initialized them 40,000 times each and printed the constant value.

    Kind Regards,
    augustas656
    Last edited by augustas656; 08-11-2015 at 10:38 AM.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    If the problem isn't with the checking of the ranges then what exactly is the problem?

    Can you write the sort of code you would like, even if it doesn't compile?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Workaround for abstract static members / class types / members pointing to subcla

    Quote Originally Posted by Tolls View Post
    If the problem isn't with the checking of the ranges then what exactly is the problem?

    Can you write the sort of code you would like, even if it doesn't compile?
    This

    Edit: I know why it doesn't compile, I'm trying to find a good analog for this. I guess though that I'm going to have to dump the idea of there being ranges at all and just stick with one universal as between 1 and 0 since java lacks this extremely useful feature...

    Kind Regards,
    Aqualight

Page 1 of 2 12 LastLast

Similar Threads

  1. class and object members!
    By andnlou2678 in forum New To Java
    Replies: 15
    Last Post: 01-31-2012, 05:25 PM
  2. Replies: 3
    Last Post: 12-27-2011, 04:17 AM
  3. Accessing abstract class subclass's data members
    By Claymz in forum New To Java
    Replies: 23
    Last Post: 04-18-2011, 11:26 AM
  4. who initializes static members?
    By munishvit in forum New To Java
    Replies: 10
    Last Post: 12-27-2010, 05:43 AM
  5. [newbie] getting class members from Arraylist
    By jon80 in forum New To Java
    Replies: 16
    Last Post: 05-15-2009, 07:45 AM

Posting Permissions

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