Can we instantiate an abstract class?

后端 未结 16 1163
长情又很酷
长情又很酷 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:17

    About Abstract Classes

    • Cannot create object of an abstract class
    • Can create variables (can behave like datatypes)
    • If a child can not override at least of one abstract method of the parent, then child also becomes abstract
    • Abstract classes are useless without child classes

    The purpose of an abstract class is to behave like a base. In inheritance hierarchy you will see abstract classes towards the top.

    0 讨论(0)
  • 2020-11-22 08:19

    Actually we can not create an object of an abstract class directly. What we create is a reference variable of an abstract call. The reference variable is used to Refer to the object of the class which inherits the Abstract class i.e. the subclass of the abstract class.

    0 讨论(0)
  • 2020-11-22 08:22

    The technical part has been well-covered in the other answers, and it mainly ends in:
    "He is wrong, he doesn't know stuff, ask him to join SO and get it all cleared :)"

    I would like to address the fact(which has been mentioned in other answers) that this might be a stress-question and is an important tool for many interviewers to know more about you and how do you react to difficult and unusual situations. By giving you incorrect codes, he probably wanted to see if you argued back. To know whether you have the confidence to stand up against your seniors in situations similar to this.

    P.S: I don't know why but I have a feeling that the interviewer has read this post.

    0 讨论(0)
  • 2020-11-22 08:22

    Extending a class doesn't mean that you are instantiating the class. Actually, in your case you are creating an instance of the subclass.

    I am pretty sure that abstract classes do not allow initiating. So, I'd say no: you can't instantiate an abstract class. But, you can extend it / inherit it.

    You can't directly instantiate an abstract class. But it doesn't mean that you can't get an instance of class (not actully an instance of original abstract class) indirectly. I mean you can not instantiate the orginial abstract class, but you can:

    1. Create an empty class
    2. Inherit it from abstract class
    3. Instantiate the dervied class

    So you get access to all the methods and properties in an abstract class via the derived class instance.

    0 讨论(0)
  • 2020-11-22 08:25

    Technical Answer

    Abstract classes cannot be instantiated - this is by definition and design.

    From the JLS, Chapter 8. Classes:

    A named class may be declared abstract (§8.1.1.1) and must be declared abstract if it is incompletely implemented; such a class cannot be instantiated, but can be extended by subclasses.

    From JSE 6 java doc for Classes.newInstance():

    InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.

    You can, of course, instantiate a concrete subclass of an abstract class (including an anonymous subclass) and also carry out a typecast of an object reference to an abstract type.

    A Different Angle On This - Teamplay & Social Intelligence:

    This sort of technical misunderstanding happens frequently in the real world when we deal with complex technologies and legalistic specifications.

    "People Skills" can be more important here than "Technical Skills". If competitively and aggressively trying to prove your side of the argument, then you could be theoretically right, but you could also do more damage in having a fight / damaging "face" / creating an enemy than it is worth. Be reconciliatory and understanding in resolving your differences. Who knows - maybe you're "both right" but working off slightly different meanings for terms??

    Who knows - though not likely, it is possible the interviewer deliberately introduced a small conflict/misunderstanding to put you into a challenging situation and see how you behave emotionally and socially. Be gracious and constructive with colleagues, follow advice from seniors, and follow through after the interview to resolve any challenge/misunderstanding - via email or phone call. Shows you're motivated and detail-oriented.

    0 讨论(0)
  • 2020-11-22 08:25

    It is a well-established fact that abstract class can not be instantiated as everyone answered.

    When the program defines anonymous class, the compiler actually creates a new class with different name (has the pattern EnclosedClassName$n where n is the anonymous class number)

    So if you decompile this Java class you will find the code as below:

    my.class

    abstract class my { 
        public void mymethod() 
        { 
            System.out.print("Abstract"); 
        }
    } 
    

    poly$1.class (the generated class of the "anonymous class")

    class poly$1 extends my 
    {
    } 
    

    ploly.cass

    public class poly extends my
    {
        public static void main(String[] a)
        {
            my m = new poly.1(); // instance of poly.1 class NOT the abstract my class
    
            m.mymethod();
        }
    }
    
    0 讨论(0)
提交回复
热议问题