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.
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:
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:
Code:
child's a: 7
child's b: 11
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.
Re: simple inheritance not working!
Quote:
Originally Posted by
digitaldrowning
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?
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.
Re: simple inheritance not working!
Quote:
Originally Posted by
digitaldrowning
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.
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
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);
}
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.