Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default simple inheritance not working!

    My code is something like this yet the integers that I put in my higher class don't transfer down to the class that is inheriting from the class that has ints.

    Class basics {
    int a;
    int b;
    }

    Class basics2 extends basics {

    }
    Doesn't inherit! Integers are not inherited! What am I doing wrong? Frustrating.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: simple inheritance not working!

    What do you mean "doesn't inherit"? The child class does hold a reference to the parent's fields, but they can't be overridden. So this works fine:

    Java Code:
    public class BasicTest {
       public static void main(String[] args) {
          BasicChild child = new BasicChild();
          System.out.println("child's a: " + child.a);
          System.out.println("child's b: " + child.b);
       }
    }
    
    class Basic {
       int a = 7;
       int b = 11;
    }
    
    class BasicChild extends Basic {
       
    }
    output:
    Java Code:
    child's a: 7
    child's b: 11

  3. #3
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: simple inheritance not working!

    I mean where u put basichild extends basic shouldn't that class inherit variables a and b? That is the prob I'm having I can't get the class to inherit the variables.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: simple inheritance not working!

    Quote Originally Posted by digitaldrowning View Post
    I mean where u put basichild extends basic shouldn't that class inherit variables a and b? That is the prob I'm having I can't get the class to inherit the variables.
    Sorry, but your question is still not clear to me. In my example above, you can access the parent fields from a variable of the child type. Can you show in code where exactly you're confused?

  5. #5
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: simple inheritance not working!

    My question is if you have two variables in one class and you extend that class to another shouldn't you be able to interact with the variables from the parent class? Maybe I am not making myself clear but in my programming book it says if u have class mammal and you have class cat extends mammal you inherit all the things that are in mammal which in my simplest attempts at that it is not working.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: simple inheritance not working!

    Quote Originally Posted by digitaldrowning View Post
    My question is if you have two variables in one class and you extend that class to another shouldn't you be able to interact with the variables from the parent class? Maybe I am not making myself clear but in my programming book it says if u have class mammal and you have class cat extends mammal you inherit all the things that are in mammal which in my simplest attempts at that it is not working.
    Yes, you still should be able to do this (as shown in my example above). Again, please show me code where this doesn't work.

  7. #7
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: simple inheritance not working!

    I will try at home with your example and give it another shot you are saying my code is correct though right? Thx for your help man

  8. #8
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: simple inheritance not working!

    Ok so i put your code in and it seems like it works backwards to what i was trying to accomplish, the parent gets to access the variables of the child. but i want to make it so the child can access the variables of the parent, make sense? my original code was:

    public class untitled {

    public static void main (String args[]) {
    int a;
    int b;
    int c;
    }
    }

    class untitled1 extends untitled {
    a = 5, b = 5;
    a + b;
    System.out.println(a,b);
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: simple inheritance not working!

    The variables define in a method are local to that method. Another method in the untitled class would not be able to see them.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Simple Graphics program not working.
    By forms in forum New To Java
    Replies: 7
    Last Post: 03-11-2012, 03:44 PM
  2. why is the most simple thing not working?
    By Majora94 in forum New To Java
    Replies: 22
    Last Post: 01-29-2012, 06:50 PM
  3. Simple code not working
    By davetheant in forum New To Java
    Replies: 7
    Last Post: 02-04-2011, 12:51 AM
  4. Simple Inheritance issue...
    By AWE in forum New To Java
    Replies: 3
    Last Post: 07-27-2009, 09:56 PM
  5. simple validations not working
    By rahaman.athiq in forum Web Frameworks
    Replies: 0
    Last Post: 11-06-2008, 06:10 AM

Tags for this Thread

Posting Permissions

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