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 2
Oldest  Newest  Rating
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();


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:
  • 1
  • 2

Java / Android Development


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

Bucky Roberts Administrator