简体   繁体   中英

Java - Abstract class with overloaded methods calling each other

I have a class that has overloaded methods.

   public class MyCal extends GregorianCalendar {

   //constructor
   public MyCal(Date date) {
     super();
     setTime(date);
   }

   boolean isSameDay(Date date) {
      return (isSameDay(new MyCal(date))) {
   }

   boolean isSameDay(MyCal cal) {
       if (...) {
         return true;
       } else {
         return false;
       }
   }

   //abstract String toString(String pattern) {};
   //if I have this I can't call new MyCal(date) from above
}

Now I want the class to be abstract (need the sub classes to implement a few other methods), and still avoid all the logic of the first isSameDay method (calling the second one). If it was just this method it would be ok I could do it, but this situation is replicated on many other overloaded methods.

The class being abstract I can't instantiate it and so the method isSameDay(date) will report an error... and really don't want to have the logic on all methods, it would make the class enormous as well as harder to maintain. Can anybody have a good way of doing this? Thank you in advance.

Remove the logic that requires instantiation of the abstract class. In your example above you can effectively do this by reversing the roles of the methods you defined:

boolean isSameDay(Date date) {
    if (...) {
        return true;
    } else {
        return false;
    } 
}


boolean isSameDay(MyCal cal) {
    if(cal == null) return false;
    return (isSameDay(cal.getTime()));
}

Extend to your other methods as needed.

You could define a method getMyCal(date)

public class MyCal extends GregorianCalendar {

    abstract MyCal getMyCal(Date date);

    // further implementation
}

and instead of using new MyCal(date) you can use getMyCal(date) in your methods.

The implementations of the abstract class have only to implement the getMyCal method.

public ConcreteCal extends MyCal {

    MyCal getMyCal(Date date) {
        return new ConcreteCal(date);
    }
}

I think that what he means is that he wants to instantiate an object of the derived class in the parent class.

I suggest doing something like this:

Parent Class

public class MyCal extends GregorianCalendar {  
(...)
boolean isSameDay(Date date) {  
   return (isSameDay(newInstance())) {  
   }  
abstract MyCal newInstance(){}
(...)
}

Derived Class

public class MyDerivedCal extends MyCal{  
(...)
abstract MyCal newInstance(){
   return new MyDerivedCal();
   }
(...)
}

Pardon my possible java syntax mistakes....

It's really hard to tell what you are asking, but I'm going to guess...

If you want to make this class abstract, but still instantiate it, you can't (you can't instantiate an abstract class), but you can do this:

public abstract class MyCal ... {
    ...
    abstract String toString(String pattern);
}

public class MyCalBasic extends MyCal {
    String toString(String pattern) {
        // some vanilla impl
    }
}

And just use MyCalBasic instead of MyCal when you want the "vanilla" version of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM