Hey lads, I have a small problem re inheritance. The 1st piece of code is the superclass which contains hour and minute. The 2nd piece of code which is the subclass, contains the int second. The 2nd piece inherits the code from the 1st piece. Now when seconds go over 59, seconds returns to zero and 1 would be added to the minute in superclass. My problem is that i'm not sure how to change the value of the private int minute from a subclass if that makes sense. I highlighted below what my problem is.

public class CurrentTime{
      private int hour;
	 private int minute;

	public void advance(CurrentTime timeToAdvance)
		this.hour = this.hour + timeToAdvance.getHour();
		this.minute = this.minute + timeToAdvance.getMinute();

                if(this.minute > 59){
                    this.minute = this.minute - 60;
                if(this.hour > 23){
                    this.hour = this.hour - 24;

public class FullTime extends CurrentTime {

    private int seconds;

    public void advance(FullTime timeToAdvance)
		this.seconds = this.seconds + timeToAdvance.getSeconds();

                if(this.seconds > 59){
                    this.seconds = this.seconds - 60;



So basically, i'm trying to call the int b (which is the int minute) from the superclass and add one to it once seconds go over 59(which is in subclass). I've put in super.b but it's obviously wrong. Does anybody have any ideas?

Edited by Bobon: n/a

8 Years
Discussion Span
Last Post by Bobon

Edit to say, "super.b++" is actually supposed to mean super.minute++. I realise this is wrong as the int minute is private in superclass. Just to clear that up. Any ideas?


Hey, the line of code that follows should do the trick

super.setMinute(super.getMinute() + 1);

but realize that the methods I invoked have yet to be defined in your classes. Neither class has methods that allow access to the data from outside of the class, the set and get methods. Once you define these methods then the line of code above will allow you to access the supers data fields without changing the private modifiers.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.