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: Java Inheritance Question...

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Java Inheritance Question...

    Hi,
    I have a stupid question. Trying to teach myself about Java Inheritance. I have the following classes:

    public class A
    {
        private int iClassVariableA;
     
        public set_class_VariableA( int iVariable)
        {
            iClassVariableA = iVariable;
        }
    }
     
    public class B extends A
    {
        private int iClassVariableB;
     
        public set_class_VariableB( int iVariable)
        {
            iClassVariableB = iVariable;
        }
    }
     
    public class C extends A
    {
        private int iClassVariableC;
     
        public set_class_VariableC( int iVariable)
        {
            iClassVariableC = iVariable;
        }
    }

    In my main I have the following:

    A[] myA = new A[3];
     
    myA[0] = new A();
    myA[1] = new B();
    myA[2] = new C()';
     
    myA[0].set_class_VariableA(0);
    myA[1].set_class_VariableA(0);
    myA[1].set_class_VariableB(1); // I get a compiler error here
    myA[2].set_class_VariableA(0);
    myA[2].set_class_VariableC(2); // and here

    The error is:
    cannot find symbol
    myA[1].set_class_VariableB(1);
    ^
    symbol: method set_class_VariableB(int)
    location: class A

    If I create each instance variable as it's own type:

    myA = new A();
    myB = new B();
    myC = new C()';

    everything works fine. My understanding of Java's inheritance says that I should be able to create a variable of class A and assign it to class B... What am I not understanding here?

    Thanks,
    Tim


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,847
    My Mood
    Mellow
    Thanks
    178
    Thanked 631 Times in 618 Posts

    Default Re: Java Inheritance Question...

    Instances of A do not even know that B and C exist. B inherits everything A has and can add to it. C inherits everything A has and can add to it. B and C have a common ancestor (A) but do not share their own methods or variables with each other.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Inheritance Question...

    Greg,
    Thanks for the response. I think I get it now...

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Java Inheritance Question...

    If you cast the object it would work.
    ((B)myA[1]).set_class_VariableB(1);


    --- Update ---

    Java Inheritance

    Duplicate post
    Improving the world one idiot at a time!

Similar Threads

  1. java - inheritance
    By Shreyas123 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 10th, 2013, 02:06 AM
  2. Inheritance question!
    By Scorks in forum Object Oriented Programming
    Replies: 4
    Last Post: September 23rd, 2013, 12:56 PM
  3. [SOLVED] Java inheritance
    By maple1100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 9th, 2013, 09:42 PM
  4. Inheritance question
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: February 24th, 2012, 06:19 AM
  5. Java Inheritance Help
    By danielparry in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 03:20 PM