简体   繁体   中英

Abstract class in Java.

Wat does it mean by indirect Instantiation of abstract class ? 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.

  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

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

Wat does it mean my indirect instanciation of abstract class ? 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. Refer this link- http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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