简体   繁体   English

Java中的抽象类。

[英]Abstract class in Java.

Wat does it mean by indirect Instantiation of abstract class ? Wat是指抽象类的间接实例化吗? how do we achieve this ? 我们如何实现这一目标? as i tried few times like .. it gives error has any one done something regarding this 正如我尝试过几次..它给错误有任何人为此做过一些事情

    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 
    }

    }

You can't instantiate an abstract class. 您不能实例化一个抽象类。 The whole idea of Abstract class is to declare something which is common among subclasses and then extend it. 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

   } 

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

我们不能实例化一个抽象类。如果想要的话,我们必须扩展它。

Wat does it mean my indirect instanciation of abstract class ? Wat是不是意味着我对抽象类的间接实例化? how do we achieve this ? 我们如何实现这一目标?

I'd need to see the context in which that phrase is used, but I expect that "indirect instantiation" means instantiation of a non-abstract class that extends your abstract class. 我需要查看使用该短语的上下文,但是我希望“间接实例化”意味着扩展抽象类的非抽象类的实例化。

For example 例如

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.

You can't create instance of abstract class, I think this is what you are trying to do. 您不能创建抽象类的实例,我想这就是您想要做的。

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

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

you cannot create object for Abstract class in java. 您不能在Java中为Abstract类创建对象。 Refer this link- http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html 请参阅此链接-http: //docs.oracle.com/javase/tutorial/java/IandI/abstract.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM