1

I'm trying to add a method to the Chronometer class in android. It stores the start time in this variable:

private long mBase;

so I thought I could do this

public class MyChronometer extends Chronometer{

    public void reset(){
        long now = SystemClock.elapsedRealtime();
        this.mBase = now;
    }
}

but Android Studio is telling me that mBase can't be found. Why is this? And what am I doing wrong? From what I understand of inheritance in Java, if I extend a class then I have all of the methods and variables of the class I extend which I can then add to. Is this incorrect? Wouldn't this include the mBase variable even though it is private?

Edit: Essentially I am trying to create a setter function for mBase

guribe94
  • 1,551
  • 3
  • 15
  • 29
  • I think it's only its protected and public members, not private. – Watermel0n Jun 27 '15 at 07:12
  • 2
    *" if I extend a class then I have all of the methods and variables of the class I extend"* The instance has them, but your derived class code can't access the private ones, just the public and protected ones. (BTW, they're "fields," not "variables.") – T.J. Crowder Jun 27 '15 at 07:19
  • 1
    There would be no point in `private` if you could do this! – Simon Jun 27 '15 at 07:31

2 Answers2

13

I'm quoting the tutorial - Private Members in a Superclass:

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

Meaning that you cannot directly access the private fields, but you can use methods that give you access to them. This table might be helpful as well:

                  Access Levels
------------+---------+---------+-----------+------
Modifier    |   Class | Package |  Subclass | World
------------+---------+---------+-----------+------
public      |     Y   |    Y    |     Y     |   Y
protected   |     Y   |    Y    |     Y     |   N
no modifier |     Y   |    Y    |     N     |   N
private     |     Y   |    N    |     N     |   N
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • What should I do then to add a setter for the private variable? Create a new variable to use? I guess I could just copy and paste and add what I want from the original class but that just feels kind of wrong. – guribe94 Jun 27 '15 at 19:22
  • No. You create a protected or public method that simply returns that field. – Maroun Jun 27 '15 at 21:57
  • thats what I was trying but I got an error, which is what led me to ask this question. If I refer to this variable at all, like if I make a public setter, it will not compile because it doesn't know what `mBuilder` is. – guribe94 Jun 28 '15 at 03:31
  • If it's private then there is a reason why it is, and probably the author don't want to give you access to it. You can use reflection or ask the author to change the modifier :) – Maroun Jun 28 '15 at 05:24
0

You need to use getters to access the private fields of a parent class.

These methods are usually used only to return the private value.

Code example of setter and getter:

Reference link: http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html

public class Printer {

    @Inject @Informal Greeting greeting;

    private String name;
    private String salutation;

    public void createSalutation() {
        this.salutation = greeting.greet(name);
    }

    public String getSalutation() {
        return salutation;
    }

    public void setName(String name) {
       this.name = name;
    }

    public String getName() {
       return name;
    }
}
Sailesh Sriram
  • 144
  • 2
  • 17
  • I understand this, what I am trying to do is add a setter method. How would I do that if the original class did not come with one? – guribe94 Jun 27 '15 at 19:21