Overriding method from parent class

Viewed 99

Trying to access the isLeapYear method in my Date parent class. I have already defined the method in the JulianDate child class. How do I correctly implement the child class to go into for the correct method?

JulianDate (child class):

class JulianDate extends Date {

    public JulianDate() {
        super(1, 1, 1);
        addDays(719164);
        addDays((int) ((System.currentTimeMillis() + java.util.TimeZone.getDefault().getRawOffset()) / 1000 / 60 / 60 / 24));
    }
    public JulianDate (int y, int m, int d) {
        super(y, m, d);
    }
    public boolean isLeapYear(int year) {
        return year % 4 == 0;
    }

This is what I have come up with in my parent class (Date) regarding isLeapYear:

Date JDate = new JulianDate();
        JulianDate.isLeapYear();

    public boolean isLeapYear() {
        return isLeapYear(this.year);
    }

What am I missing in my parent and child class to make this work? I want the child class to calculate leap year and override the parent method.

3 Answers

A child class will inherit all of its parent class's methods. You don't need to define them again in the child class. So if you want the parent class(Date) to check leap year, then define the method in Date class, and remove the isLeapYear method from JulianDate. If you want the child class to check it, you need to override method of the parent class.

Is the last piece of code from selfmade Date class?, if so it doesn't seem to be defined correctly. Its missing a constructor and for some reason have a method call, which it shouldn't.


Added after comment

class Parent {
int number;

    public Parent (int number) {
    this.number = number;
    }
    public int getNumber ()  {
    return number;
    }
}

class Child extends Parent {
super() {
}
@override
public int getNumber () {
return this.number;
}

}


Child demo = new Child(10);
demo.getNumber(); //uses the child method getNumber.

however i would just drop the getNUmber(or leapyear in your case) from the parent and only have a function for it in the child class. By putting the same function into both classes you are duplicating code which isnt good practice.

class JulianDate extends Date {

public JulianDate(){
    super(1, 1, 1);
}

public JulianDate(int y, int m, int d) {
    super(y,m,d);
}

public boolean isLeapYear() {
    boolean leap = false;
    if(getYear() % 4 == 0)
    {
        if( getYear() % 100 == 0)
        {
            // getYear() is divisible by 400, hence the getYear() is a leap getYear()
            if ( getYear() % 400 == 0)
                leap = true;
            else
                leap = false;
        }
        else
            leap = true;
    }
    else
        leap = false;

    return leap;
}

public boolean isLeapYear(int y) {
    this.setYear(y);


      return  this.isLeapYear();
   }
    public static void main(String [] ar){

    JulianDate julianDate= new  JulianDate(1988, 02, 21);

    System.out.println(julianDate.isLeapYear());

    JulianDate julianDate2= new  JulianDate();

    System.out.println(julianDate2.isLeapYear(1988));


 }
}

Hope this will help you.

class JulianDate extends Date {

 public JulianDate() {}

 public boolean isLeapYear(int year) {
    return year % 4 == 0;
 }
}

and you can use it like,

Date date = new JulianDate();
boolean isLeapYear = date.isLeapYear(2020);
Related