Can we instantiate an abstract class?

后端 未结 16 1394
长情又很酷
长情又很酷 2020-11-22 07:43

During one of my interview, I was asked \"If we can instantiate an abstract class?\"

My reply was \"No. we can\'t\". But, interviewer told me \"Wrong, we can.\"

16条回答
  •  执念已碎
    2020-11-22 08:11

    Abstract classes cannot be instantiated, but they can be subclassed. See This Link

    The best example is

    Although Calender class has a abstract method getInstance(), but when you say Calendar calc=Calendar.getInstance();

    calc is referring to the class instance of class GregorianCalendar as "GregorianCalendar extends Calendar "

    Infact annonymous inner type allows you to create a no-name subclass of the abstract class and an instance of this.

提交回复
热议问题