DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2004

    Inheritance program

    Hello I am very new to Java I have clock class and I would like to make a Derived class from the clock class here is what I have so far. But I am having trouble understanding how to do this. I looked for some code online but found none. Any help in writing this would be greatly appreciated.

    public class Clock
    private int hour;
    private int minute;
    private int second;

    public Clock()
    //initialize to default values
    hour = 0;
    minute = 0;
    second = 0;
    public Clock(int h, int m, int s)

    /* Valid integers for hour is in the range of 0 24 */
    public void setHour(int h)
    if((h >= 0) && (h <= 24))
    hour = h;
    else System.out.println("Fatal error: invalid hour");
    /* Valid integers for minute is in the range 0 59 */
    public void setMinute(int m)
    if((m >= 0) && (m <= 59))
    minute = m;
    else System.out.println("Fatal error: invalid minute");
    /* Valid integers for second is in the range 0 59 */
    public void setSecond(int s)
    if((s >= 0) && (s <= 59))
    second = s;
    else System.out.println("Fatal error: invalid second");

    public int getHour()
    return hour;
    public int getMinute()
    return minute;
    public int getSecond()
    return second;

    //Facilitator methods
    public String toString()
    return "The current time is: " + hour + ":" + minute + ":" + second;

    public boolean equals(Object o)
    if (0 == null)
    return false;
    else {
    if (getClass() != o.getClass())
    return false;
    else {
    Clock otherClock = (Clock) o;
    return((hour == otherClock.hour) &&
    (minute == otherClock.minute) &&
    (second == otherClock.second));
    I have these for the derived class:

    public class AlarmClock extends Clock

    public AlarmClock()

    public AlarmClock(h,m,s,ah,am,as)

    public void setAlarmHour(int h)

    get hours ()

    get Minutes ()

    get Seconds()

    set hours()

    Set Minutes()

    Set seconds()

    the meat of the methods are giving me trouhle

  2. #2
    Join Date
    Dec 2004
    Well that's really great clok class you got there
    it's cool to have Alarmclocks
    you just need to use
    the keyword extends Clock
    and have a clear idea of how alarmclock are different from clock
    and eventually creat a methode on the alarmclock class that would hightlight those diffrences
    good luck

  3. #3
    Join Date
    Feb 2004
    When you extend a class you don't need to implement all of the super class methods again. If the methods do exactly the same think in the child class as they do in the parent class then you just leave them alone, they will be inherited. With your alarm clocks, the getting and setting of the time will be exactly the same as in the normal clock. You don't need to overide the methods getHour and setHour etc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.