cannot be instantiated because it is abstract but it's not abstract.
public class Solution {
public static void main(String[] args) {
System.out.println(new Dream().HOBBY.toString());
System.out.println(new Hobby().toString());
}
interface Desire {
}
public static interface Dream {
public static Hobby HOBBY = new Hobby();
}
static class Hobby implements Dream, Desire {
static int INDEX = 1;
@Override
public String toString() {
INDEX++;
return "" + INDEX;
}
}
}
com.codegym.task.task13.task1305.Solution.Dream is abstract; cannot be instantiated. file com/codegym/task/task13/task1305/Solution.java, line 12, position 165
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
4 November 2019, 15:20
Abstract means that the particular object can be be declared but cannot be instantiated. Interfaces are abstract by definition.
Code like this will not work:
While code like this is perfectly acceptable:
In the code you linked above, the line that is causing problems is "System.out.println(new Dream().HOBBY.toString());" specifically the new Dream() part. That is because it is trying to create an object out of an interface (which is abstract). Because the variable HOBBY in the interface is static, you can access it without creating an object. Try doing this. +4
Steve Bennett
17 November 2020, 16:53
You always give the best answer, not the exact answer but enough to solve! Helped me straight out, thanks !!
+1