Can we instantiate an abstract class? Is it possible?
本问题已经有最佳答案,请猛点这里访问。
我在SCJP6中看到抽象类不能被实例化。但是从某个地方读到它们可以被实例化。是真的吗?我是Java新手,希望有人能详细解释一下。举个例子也不错。
你可以创建一个参考在抽象类,但它instantiate斜面。的EC。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public abstract class AbstractClass { public abstract void abstractMethod(); public void concreteMethod(){ System.out.println("am in concreteMethod"); } } public class ExtndClass extends AbstractClass{ @Override public void abstractMethod() { // TODO Auto-generated method stub System.out.println(" am in extended class"); } public static void main(String...arg){ AbstractClass abs = new ExtndClass(); abs.abstractMethod(); abs.concreteMethod(); } } |
输出:
1 2 | am in extended class am in concreteMethod |
从Java的文档:
Abstract classes cannot be instantiated, but they can be subclassed.
Abstract classes are similar to interfaces. You cannot instantiate them...