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();
        
        appleObject.food();
        
        System.out.println(cookie);
    //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? 

-Thanks!8-)

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+2 Mathias Frits Rørvik · September 23, 2014
Yes, you can use inheritance.  

/* This program prints 1241 */ 

class Food {
    int size = 1241;
}

class Apple extends Food {
    Apple() {
System.out.println(this.size);
    }
}

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

+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)
+1 Mathias Frits Rørvik · September 23, 2014
You're declaring cookie locally in a method. 
You're not extending any classes, where is your super class?

Stop using Bucky's idiotic naming conventions. Naming all your variables and classes Tuna, Bacon, cookie, sandwich etc. makes your code hard to understand. This is one big reason why I dislike the Java videos and discourage people to use them. 

Object oriented programming is about modelling the real world, not making up random attributes. Why would an apple have a cookie?
+1 Patrick Lehmann · September 23, 2014
Besides the way he names the variables

There is, at least as far as I know, no way to inherit variables within Java. No Pointers no nothing. A simple way to do this would be via getters

So your new Apple class would look something like this

package me.wizard7611.Inheritance;

public class Apples {
   
   private int cookie;

   private void food(){
       this.cookie = 4;
   }

public int getCookies() {
  return this.cookie;
  }
}



and in your Tuna class


package me.wizard7611.Inheritance;

public class Tuna{
   
   public static void main(String[] args){
       
       Apples appleObject = new Apples();
       
       appleObject.food();
       // Call the getter to "get" the variable
       int cookie = appleObject.getCookies();
       System.out.println(cookie);
   // Now this will print out 4
   }

}
+1 Mathias Frits Rørvik · 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 30, 2014
Mathias Frits Rørvik:

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

Source:

_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-


/* This program prints 1241 */ 

class Food {
   int size = 1241;
}

class Apple extends Food {
   Apple() {
System.out.println(this.size);
   }
}

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

_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
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 23, 2014
Lol calm down, I was just doing that for a simple test, not logically doing it or following Bucky (Greg).
0 Developer John · September 23, 2014
The reason I'm asking this here is because whenever someone creates a program, they make several classes for it. Now, are those inner classes, or outer classes? Do you only need one class when you're extending a certain API?
0 Mathias Frits Rørvik · September 23, 2014
They are public classes. An inner class, is a class defined inside another class. For API's it depends on the API and your design pattern.
  • 1
  • 2

Java / Android Development

107,299 followers
About

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

Links
Moderators
Bucky Roberts Administrator