简体   繁体   中英

Cannot instantiate an abstract class

package InterfaceAbstractOverloadingOverriding;

public class instrumentExecute 
{

    public static void main(String[] args) 
    {
         GuitarAbstract g = new GuitarAbstract();
         NewGuitar ng = new NewGuitar(); 
         g.play();
         ng.play();

         g = new GuitarAbstract(7);
         ng = new NewGuitar(5);
         g.play();
         ng.play();
    }
}

I'm not able to instantiate the GuitarAbstract class

Error:

Cannot instantiate the type GuitarAbstract.GuitarAbstract is an abstract class.

You can't directly instantiate an abstract class, but you can instantiate an anonymous class for your abstract class.

For example, given the following abstract class:

class GuitarAbstract {

  public abstract void play();
}

You can create an anonymous class like so:

GuitarAbstract guitar = new GuitarAbstract() {

  @Override
  public void play() {
    System.out.println("Playing guitar!");
  }
};

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