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;
                    hour++;
                }
                if(this.hour > 23){
                    this.hour = this.hour - 24;
                }
    }


}
public class FullTime extends CurrentTime {

    private int seconds;

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

                if(this.seconds > 59){
                    this.seconds = this.seconds - 60;
                    [B]super.b++;[/B]
                }

	}

}

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?

Recommended Answers

All 3 Replies

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.

Hey Rue, thanks a million. Got that sorted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.