Java中的抽象类。

时间:2014-04-12 06:59:54

标签: java scjp

Wat是否通过间接实例化抽象类来表示?怎么做 我们做到了吗? 因为我尝试了几次像..它给出错误,任何人都做了一些关于这个

    abstract class hello //abstract class declaration 
    { 
    void leo() {}
    }             


    abstract class test {} //2'nd abstract class 


    class dudu {  //main class 

    public static void main(String args[]) 
    {
    hello d = new test() ;  // tried here 
    }

    }

5 个答案:

答案 0 :(得分:2)

您无法实例化抽象类。 Abstract类的整个思想是声明子类中常见的东西然后扩展它。

  public abstract class Human {
         // This class can't be instantiated, there can't be an object called Human
         }

  public Male extends Human {
         // This class can be instantiated, getting common features through extension from Human class
   } 

  public Female extends Human {
        // This class can be instantiated, getting common features through extension from Human class

   } 

更多信息:http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

答案 1 :(得分:2)

我们无法实例化一个抽象类。如果我们想要,我们必须扩展它。

答案 2 :(得分:2)

  

Wat是否意味着我对抽象类的间接实例化?我们如何实现这一目标?

我需要查看使用该短语的上下文,但我希望“间接实例化”意味着实例化扩展抽象类的非抽象类。

例如

public abstract class A {
    private int a;
    public A(int a) {
       this.a = a;
    }
    ...
}

public B extends A {
    public B() {
        super(42);
    } 
    ...
}

B b = new B();    // This is an indirect instantiation of A
                  // (sort of ....)

A a = new A(99);  // This is a compilation error.  You cannot
                  // instantiate an abstract class directly.

答案 3 :(得分:1)

你不能创建抽象类的实例,我想这就是你想要做的。

abstract class hello //abstract class declaration 
{ 
    void leo() {}
}             

class test extends hello 
{
    void leo() {} // Custom test's implementation of leo method
}

答案 4 :(得分:1)

你不能在java中为Abstract类创建对象。 请参阅此链接 - http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html