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
+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
   }

}
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 Developer John · September 24, 2014
Oh yea, I didn't read the extends part. I guess they are public classes.
0 Developer John · September 24, 2014
Actually, I should ask, what does this do:
new Apple();
0 Developer John · September 24, 2014
So basically, you're making an appleObject, but not assigning it a name?
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
@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();
+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 25, 2014
Thank you for organizing all that up.8-)
  • 1
  • 2

Java / Android Development

129,867 followers
About

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

Links
Moderators
Bucky Roberts Administrator