Inheritance For Variables

0 Developer John · September 23, 2014
Ok, before I show you my code, remember, I don't know how to inherit variables. I don't know if it is different from inheriting strings or not. With that being stated, here is my main class called Tuna:
package me.wizard7611.Inheritance;

public class Tuna{
    public static void main(String[] args){
        Apples appleObject = new Apples();
    //System.out.println(cookie); gives me an error saying "Cookie cannot be resolved as a variable"


My subclass:
package me.wizard7611.Inheritance;

public class Apples {
    private void food(){
        int cookie = 4;


Why exactly do I get the error and how can I solve it? 


Post a Reply


- page 1
Oldest  Newest  Rating
0 Developer John · September 30, 2014
Ok so the main method calls the Apple class's method, but still uses it for the future, or does the program really funtion in the super class? Food class is where you store the variable size(subclass). The Apple class extends the Food class to get the variable size and prints it (super class). You stated that 
new Apple();

is used when you're not using the object for the future, yet you use the size variable in the super class. I thought you can only do this in the main class. I also forgot to mention, do you always need a subclass? Hmm...:ermm::blink::sideways::wassat:
0 Developer John · September 30, 2014
Mathias Frits Rørvik:

Remember to put the main method first. After all, it is the MAIN METHOD lol.8-)



/* This program prints 1241 */ 

class Food {
   int size = 1241;

class Apple extends Food {
   Apple() {

class Test {
   public static void main(String[] args) {
new Apple();


+1 H. P. Lovecraft · September 25, 2014
Yes, basically. Although the technical term is to "create a reference to an object", not assign a name to an object.l =)
0 Developer John · September 25, 2014
Thank you for organizing all that up.8-)
+2 Brandon Elliott · September 24, 2014
yes, as he stated, those lines do the same thing, except for the second one stores the object for future use. if you just do

new Apple();

it will create a new Apple object but you can't do anything else with it after. whatever is in the Apple constructor will run and that's that. but sometimes that's all you need (like in his example)

when you get into actually creating a large program, the thing is, you don't want to have all (or even a lot) of your code in your main method. what you want to do is have classes that do the work, and in your main method you want to utilize those classes. sometimes all you want to do is create a new instance of a class and it will do all the work. (again, like in his example)
0 Developer John · September 24, 2014
@Koen Prins No, what you do is create an object for the Apple class. The objects name is grannySmith. I still don't understand what this means:
new Apple();

1. How do you use it properly?
2. Is it a substitute for:
Apple appleObject = new Apple();
0 Koen Prins · September 24, 2014
I am pretty new to this, but wouldn't the variable new be grannySmith?
0 Developer John · September 24, 2014
So basically, you're making an appleObject, but not assigning it a name?
0 H. P. Lovecraft · September 24, 2014
It creates an instance of Apple, but I don't store it in a reference.

//These lines do the same
new Apple();
Apple grannySmith = new Apple();
//but I store one Apple object in a reference called grannySmith
0 Developer John · September 24, 2014
Actually, I should ask, what does this do:
new Apple();
  • 1
  • 2

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator