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 2
Oldest  Newest  Rating
0 Developer John · September 24, 2014
Oh yea, I didn't read the extends part. I guess they are public classes.
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.
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 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).
+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();
    }
}

+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 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
  • 2

Java / Android Development

107,183 followers
About

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

Links
Moderators
Bucky Roberts Administrator