Results 1 to 11 of 11
  1. #1
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Unhappy Java Inheritance

    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
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Java Inheritance

    When posting code please wrap it in [code] tags [/code].

    As far as the compiler is concerned you can only access methods that exist on the class or interface that a variable has been declared as.
    In the case of your array it is an array of class A, so the variable only has access to methods that have been declared in A, no matter what type of object it is actually pointing at.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Re: Java Inheritance

    Tolls,
    I took my example from the web where there was and animal class, that was extended to a Dog class and a Cat class. In that example, a variable was created as and Animal and assign as a new Dog. Thereafter any of the Dog methods were able to be used. I am trying the same thing. It lets me do the assignemnt but not call any of the methods...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Inheritance

    It's the compiler: all it knows is that the myA array contains A objects; you are trying to call a type B or C method from a type A object; inheritance is a runtime thing (as you have noticed).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Re: Java Inheritance

    JosAH,
    I understand that it's the compiler. But does that mean that the examples I found around the web are incorrect? Or do I just need to use another compiler? I am currently using jGrasp...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Java Inheritance

    I suspect you have misunderstood the examples.
    Without seeing them it's hard to tell...though if they came from roseindia then they are probably rubbish.

    Note, jGrasp is an IDE (like Eclipse, Netbean etc)...it's not a compiler.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Re: Java Inheritance

    Tolls,
    Can you give e a simple example where I can assign a base class variable to a child class and be able to use the child class's methods?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Inheritance

    Quote Originally Posted by tim8w View Post
    Tolls,
    Can you give e a simple example where I can assign a base class variable to a child class and be able to use the child class's methods?
    Only if those methods in the derived class are also defined in the base class (they needn't do anything there, you override them anyway in the derived class(es) anyway).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Java Inheritance

    Here.

    Java Code:
    public class InheritanceDemo {
       public static void main(String[] args) {
          ParentClass pc = new ChildClass();
          pc.method1(); // calls child method
          pc.method2(); // calls child method
          pc.method3(); // calls parent method
        
          // method4 not visible to ParentClass
    //      pc.method4(); // won't compile
       }
    }
    
    class ParentClass {
       public void method1() {
          System.out.println("I am method1 - from parent class");  
       }
       public void method2() {
          System.out.println("I am method2 - from parent class");      
       }
       public void method3() {
          System.out.println("I am method3 - from parent class");
       }
    }
    
    class ChildClass extends ParentClass {
       public void method1() {
          System.out.println("I am method1 - from child class");
       }
       public void method2() {
          System.out.println("I am method2 - from child class");
       }
       public void method4() {
          System.out.println("I am method4 - from child class");
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Re: Java Inheritance

    Thaks for alll the responses. I think I understand it better now...

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

Similar Threads

  1. Java Help - Inheritance and OOP
    By EdOBannon in forum New To Java
    Replies: 4
    Last Post: 11-06-2011, 04:48 AM
  2. Java Help - Inheritance
    By EdOBannon in forum New To Java
    Replies: 5
    Last Post: 11-06-2011, 03:18 AM
  3. Inheritance in Java
    By socboy6579 in forum New To Java
    Replies: 2
    Last Post: 02-08-2011, 05:40 PM
  4. Help with java gui and inheritance
    By sssss in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2011, 05:38 AM
  5. Multiple Inheritance in java
    By paty in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 03:25 PM

Posting Permissions

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