0

I just learned today just a few minutes ago that I can nest a number of static classes within a containing class which I wasn't aware of. I got used with creating classes with no nested classes at all in the past.

I thought that making use of nested classes will help in readability of codes especially when an object or a class has subclasses or types, just like Payment where you need to consider paymentterms.

I find that understanding nested classes and applying it to my coding is very powerful combined with interfaces.

So I tried to apply it to my current project where I design the classes and methods of Payment.

public class Payment {

    public static class terms{

        public static class monthly implements Monthly{

            @Override //error here
            public static void setDownpayment(double aDownPayment) //and error here
            {

            }

        }

        public static class quarterly{
            public static void setDownpayment(){
                //do something
            }
        }

        public static class semestral{
            public static void setDownpayment(){
                //do something
            }
        }
    }
} 

and here's the interface I created

public interface Monthly {
    public void setDownpayment(double aDownPayment);
}

I tried to make the setDownpayment() method to be static so I can refer to it like this:

Payment.terms.monthly.setDownpayment(aDecimalValue);

But it doesn't seem to allow static methods. because there's an error on the 2 lines I commented with "//error here and //and error here"

How do I fix it?

Any other possible solution or alternative ways or design suggestions?

I'd appreciate any help.

Thanks.

heisenberg
  • 1,784
  • 4
  • 33
  • 62
  • 1
    First thing that jumps out: you cannot override static methods. – azurefrog Aug 18 '16 at 17:23
  • A litte off-topic from your question, but you might consider the issue of [Why not use Double or Float to represent currency?](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Markus Mitterauer Aug 18 '16 at 17:37
  • which version of Java do you use? – Nicolas Filotto Aug 18 '16 at 17:38
  • Names of Java classes should alwas start with an upper-case letter. – Markus Mitterauer Aug 18 '16 at 17:40
  • Maybe it's just unfortunate naming, but I do not understand the interface `Monthly`. An interface should be an abstract description of features that are common to concrete instances. I do not see sense in creating an interface `Monthly` as I don't see any more concrete options. – Markus Mitterauer Aug 18 '16 at 17:48

1 Answers1

2

In java, You have to override all methods of an implemented Interface, and as static methods are part of classes not Objects and they are not overridable that's why static methods inside interfaces are not allowed (for before java8 ).

In Java8, static method in Interface is allowed but they must have body inside the interface and you can't override them inside the implementation classes.

void
  • 7,760
  • 3
  • 25
  • 43