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

Thread: Abstract Classes and Named Constants

  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 Abstract Classes and Named Constants

    Hello, everyone!

    I have a quick question relating to... well, if you read the title you already know.

    If I had access to an IDE right now, I would check this quick, but I do not. Anyways, I best explain myself through example, so:

    public abstract class AbstractSuperclass
    {
       //have abstract named constants
       private static abstract final int CONSTANT_A;
       private static abstract final int CONSTANT_B;
    }
    public class NonAbstractSubclass extends AbstractSuperclass
    {
       //constants given values
       CONSTANT_A = 5;
       CONSTANT_B = 2;
    }

    Is the above code legal? To put it in words, can I define abstract named constants in an abstract class, and then give those constants values in a non-abstract subclass of that abstract 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.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    963
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Abstract Classes and Named Constants

    Member variables can't be abstract. Also you can't assign them later is they are marked "final". Or if they are "private", since in that case they won't be visible. Finally the assignment of values must be within some sort of block (including constructor or method).

    So you end up with something like:

    public abstract class AbstractSuperclass
    {
       static protected int CONSTANT_A;
       static protected int CONSTANT_B;
    }
     
    class NonAbstractSubclass extends AbstractSuperclass 
    {
        static 
        {
            CONSTANT_A = 5;
            CONSTANT_B = 2;
        }
    }

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    snowguy13 (March 7th, 2012)

  4. #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: Abstract Classes and Named Constants

    Member variables can't be abstract.
    Okay. I understand that the example was poor; it was meant to demonstrate a simple idea, nothing more.

    It appears I will go back to the drawing board.
    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.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    963
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Abstract Classes and Named Constants

    Yes - I wondered whether, by removing the "final", I was gutting your example of what you had hoped would be its usefulness. Perhaps it would help if you said how you want the classes to behave or what they ought to do.

Similar Threads

  1. Interfaces Vs Abstract classes
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 08:49 AM
  2. Issue with abstract classes
    By zaphod2003 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2011, 02:25 AM
  3. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 07:15 AM
  4. class constants instance constants..... etc
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2009, 04:38 PM
  5. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 12:45 PM