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: Using an array which is in a superclass

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using an array which is in a superclass

    Ok so I have these two classes: Book and Encyclopedia. I want to create a method in Encyclopedia. I want it to modify an array which is an object in Book class. I'm not allowed to take the array as a method input like this: public void MethodName(Book [] a, String ...){ Instead, I have to find a way to call that array in the method and modify one of the elements in it. I know how to modify the element, just I don't know how to call the array in a superclass to a subclass. Thanks.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Using an array which is in a superclass

    In order to directly edit a variable from a superclass, you need to set it protected. Here is the gist of the modifiers:
    • public: Anyone can use the variable.
    • no modifier: Anyone in this package can use this variable.
    • protected: Sub-classes can use this variable
    • private: Only this class can use this variable


    So let's say you have a "Beverage" and a "Soda" class. Where Soda extends Beverage

    Beverage:
    /**
     * A classic beverage with a name. Not inheritantly mutable.
     */
    public class Beverage
    {
      //Variables
      /**
       * The name of the Beverage
       */
      protected String name;
      //Constructors
      /**
       * Specifies the name of the beverage.
       * @param nameOfBeverage The name of the beverage
       */
      public Beverage(String nameOfBeverage)
      {
        name = nameOfBeverage;
      }
      //Methods
      /**
       * @return name of the beverage
       */
      public String getName()
      {
        return name;
      }
    }
    Soda:
    /**
     * Extension of Beverage whom's name is either Coke, Dr. Pepper, or Root Beer.
     */
    public class Soda extends Beverage
    {
      //Constructors
      /**
       * Create a beverage of the specified type. 0, 1 and 2 being Coke, Dr. Pepper and Root Beer respectively.
       */
      public Soda(int type)
      {
        /* First get the String value of the type.  I do believe it is only possible to call
         * the super-classes constructor as the first line of code, so we modifiy the variable
         * directly.
         */
        String name = null;
        switch(type)
        {
          case 0:
            name = "Coke";
            break;
          case 1:
            name = "Dr. Pepper";
            break;
          case 2:
            name = "Root Beer";
            break;
        }
        //Call our parent classes variable "name" and sets it.
        super.name = name;
      }
    }

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using an array which is in a superclass

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/50499-using-array-superclass.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Assigning a Value to a Class extending a Superclass
    By ubermoe in forum Object Oriented Programming
    Replies: 9
    Last Post: September 22nd, 2011, 11:17 AM
  2. Problem with subclass and superclass methods
    By Farmer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2011, 08:43 PM
  3. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM
  4. [SOLVED] Invoking a superclass constructor in my subclass
    By kari4848 in forum Object Oriented Programming
    Replies: 5
    Last Post: April 29th, 2011, 01:12 PM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM