Home > Enterprise >  Why can't I call a method (public or static) from an Abstact class of type subclass when creati
Why can't I call a method (public or static) from an Abstact class of type subclass when creati

Time:10-15

In the code below the function dollar of type Dollar is not found and the program doesn't compile:

    class Test{
      abstract class Money{
        protected int amount;
        public Dollar dollar(int amount){
          return new Dollar(amount);
        }
    }

    class Dollar extends Money{
      public Dollar(int amount){
        this.amount= amount;
      }
    }

    public void testMultiplication(){
      Money d = new Money.dollar(5);
    }

    public static void main(String args[]){}
}
    
    

I've launched my java app on the cli with the command:

java File.java 

Here's what the warning says:

File.java:16: error: cannot find symbol
        Money d = new Money.dollar(5);
                       ^
  symbol:   class dollar
  location: class Test.Money
1 error
error: compilation failed 

The code is referenced from the book Test-Driven Development By Example, written by Kent Beck. And there's a github with the more complete code that I am using as a guide: https://github.com/No3x/money-tdd-by-example-kent-beck/blob/master/src/main/java/de/no3x/tdd/money/Money.java

I want to mention that the function Dollar dollar in the abstract class Money is also instructed to be static but that creates another error on top of this current error we're dealing with here.

On reading the above can you see why I can't call a method (public or static) from an Abstact class of type subclass when also creating an instance of an object in another function?

CodePudding user response:

TLDR: There are a lot of problems with this code, if it's copied from a book then I suggest you start again from scratch. The problem isn't with abstract classes.

The problems:

First of all, Money d = new Money.dollar(5); is not valid syntax. If method A creates a new object of type B, you should write: B justCreated = A(); (lose the new).

Other than that: in your version (the function Dollar dollar in the abstract class Money is also instructed to be static), since dollar is non-static, you can't use Money.dollar().

If it was static, it still doesn't work (as you said), because you can't make a static method inside a non static inner class, so Money must be static as well. But then you can't use new inside a static method (see this thread).

CodePudding user response:

  • abstract class Money : You're declaring an abstract class Money.
  • Money d = new Money.dollar(5); : Then you're trying to instantiate an object from this class with new keyword. This is not allowed for abstract classes.

If you want to call a method defined in an abstract class from outside then it must be a static method, then call this method statically. Here is a refactored version of your code that works.

class Test {

   abstract static class Money {

    protected int amount;

    public static Dollar dollar(int amount) {
      return new Test().new Dollar(amount);
    }
  }

  class Dollar extends Money {

    public Dollar(int amount) {
      this.amount = amount;
    }
  }

  public void testMultiplication() {
    Money d = Money.dollar(5);
  }

  public static void main(String args[]) {}
}
  • Related