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

Thread: Accessing another abstract class's variable?

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Accessing another abstract class's variable?

    Hello guys, I just started self teaching and encounter a problem that requires me to access a variable from a different class.

    Here's a shortened version of what I have so far...

    abstract public class A
    {
         //nothing yet
    }
     
    public class B extends A
    {
        protected int num;
        public int getNum(){...}
        public void setNum(int x){...}
    }
     
    abstract public class XYZ
    {
        protected A[] myList = new A[10];  // this is an array of instances of class A
    }
     
    public class XYZ_2 extends XYZ
    {
        public int small()
        {
            // here I want to be able to compare each element in 'myList' to find the smallest number
            // but right now I don't even know how to access the variable 'num'...
     
            int x = this.myList[0].num           //attempt 1
            int x = this.myList[0].getNum();     //attempt 2
            int x = myList[0].num                 //attempt 3
            int x = myList[0].getNum();           //attempt 4
     
            // the error says "XYZ_2.java:141:error:cannot find symbol"
            //                "int x = this.myList[0].getNum();"
            //                                  ^
            //                "symbol: method getNum();"
            //                "location: class A"
            //                "1 error" 
        }
    }

    Any advice is HUGELY appreciated..!!
    Last edited by BambiWithPMS; February 13th, 2019 at 04:06 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,796
    Thanks
    52
    Thanked 2,329 Times in 2,285 Posts

    Default Re: Accessing another abstract class's variable?

    If you are getting compiler error messages, please copy them and paste them here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing another abstract class's variable?

    Oh! I didn't know that. Thank you! It is fixed now.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,796
    Thanks
    52
    Thanked 2,329 Times in 2,285 Posts

    Default Re: Accessing another abstract class's variable?

    "XYZ_2.java:141:error:cannot find symbol"
    // "int x = this.A[0].getNum();"
    // ^
    // "symbol: method getNum();"
    The compiler can not find a method named getNum that is defined in the class A.

    Make sure the method is defined in the class before trying to call it.

    In the posted code, getNum is defined in class B
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing another abstract class's variable?

    I see the mistake in my posted code and fixed it again. Sorry for the confusion!

    Is there a way to call the getNum() method in class B? My class A is an abstract class and the exercise forbids me to make any changes to class A

    Side note: myList is an array of objects of class A, and I plan to put objects of class B, which extends class A, into the array later on.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,796
    Thanks
    52
    Thanked 2,329 Times in 2,285 Posts

    Default Re: Accessing another abstract class's variable?

    way to call the getNum() method in class B
    Create an instance of the B class and use the reference to that instance to call the method:
       SomeClass smCls = new SomeClass();    // create an instance and save reference to it
       smCls.theMethod();   // call the method in the class using the reference
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Variables equaling null in some class's but not others
    By frozen java in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 4th, 2012, 12:21 PM
  2. First time poster looking for help accessing a variable from a separate class.
    By royalcrown28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 20th, 2012, 12:41 AM
  3. Linking class's
    By Tate in forum Object Oriented Programming
    Replies: 3
    Last Post: February 23rd, 2012, 04:12 AM
  4. Abstract class variable help
    By star12345645 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2012, 08:40 AM
  5. Replies: 1
    Last Post: October 11th, 2011, 10:55 AM

Tags for this Thread