This site requires JavaScript, please enable it in your browser!
Greenfoot back
Baenefin
Baenefin wrote ...

2014/2/6

How to acces a private value from another class

Baenefin Baenefin

2014/2/6

#
Hello! I have a class that contains a private value, say waarde. My supclass needs to get to that waarde and multiply it by 2. I can't seem to figure out how to get to the value of lamp from my main class.
public abstract class Kaart extends Actor
{
    private int waarde;
    

    public Kaart(int waarde)
    {
        this.waarde = waarde;
    }
    
    public int getWaarde()
    {
        return waarde;
    }
public class Hart extends Kaart
{
   
   public Hart (int waarde)
   {
       super(waarde);
    }
}
how can i get the waarde and multiply it?
danpost danpost

2014/2/6

#
Baenefin wrote...
I can't seem to figure out how to get to the value of lamp from my main class.
I presume that 'waarde' is lamp; but, what is the name of your 'main' class?
Gevater_Tod4711 Gevater_Tod4711

2014/2/6

#
There are serval ways for doing this. It depends on what kind of variable waarde is. E.g. if it has the same value for every instance of the class you could declare it static and use a getter method to access the value from your subclass. Or if you don't want the variable to be visible from other classes but be inherited (so it exists in the subclasses) you can delcare it protected instead of private.
You need to login to post a reply.