2

I went to an interview. Interviewer asked me if one can instantiate an interface and abstract class? As per my knowledge I said "No". But he said "Yes, we can with the help of an anonymous class".

Can you please explain to me how?

Philip Allgaier
  • 3,505
  • 2
  • 26
  • 53
Däñish Shärmà
  • 2,891
  • 2
  • 25
  • 43
  • 1
    No you cannot instantiate non-concrete classes. But you can of course create an anonymous class and instantiate that. – Boris the Spider Aug 29 '15 at 15:57
  • 3
    You were correct, interviewer was wrong. You can't instantiate an interface or an abstract class. You can create an implementing class / subclass, which can be instantiated, whether anonymous or named. – Andreas Aug 29 '15 at 16:00
  • FYI: Just tried to google `java anonymous "create instantiation"` to see if that phrase is used anywhere, and the only real hit was two links to *this question*. Dang Google is fast. And interviewer is wrong. – Andreas Aug 29 '15 at 16:22
  • **Philip Allgaier!** You just changed the wording of the question. The wording was important to the interpretation, so bad edit in my book. – Andreas Aug 29 '15 at 16:24
  • possible duplicate of [Interview: Can we instantiate abstract class?](http://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class) – RamanSB Aug 29 '15 at 17:02

6 Answers6

5

This was a trick questions.

No you can not instantiate an interface or abstract class.

But you can instantiate an anonymous class that implements/extends the interface or abstract class without defining a class object. But it is just a shortcut to defining a fully named class.

So I would say technically your answer was correct.

Torge
  • 2,174
  • 1
  • 23
  • 33
  • It all depends on what you mean by "create instantiation". I read it as instantiating it directly, not by using anonymous or named class, so I'm with OP. Interviewer was wrong or not clear enough in the question. Up-vote to this answer. – Andreas Aug 29 '15 at 16:03
3

I don't know what is "instantiation of interface and abstract class". I think it's an inaccurate, improper expression of something, we can only guess at the intended meaning.

You cannot create an instance of an interface or an abstract class in Java.

But you can create anonymous classes that implement an interface or an abstract class. These won't be instances of the interface or the abstract class. They will be instance of the anonymous class.

Here's an example iterator from the Iterator interface that gives you an infinity of "not really":

new Iterator<String>() {
    @Override
    public boolean hasNext() {
        return true;
    }

    @Override
    public String next() {
        return "not really";
    }
};

Or a funky AbstractList that contains 5 "not really":

    List<String> list = new AbstractList<String>() {
        @Override
        public int size() {
            return 5;
        }

        @Override
        public String get(int index) {
            return "yes";
        }
    };
janos
  • 120,954
  • 29
  • 226
  • 236
0

Assume you have an abstract class: MyAbstractClass with abstract void method myAbstractMethod. Then you can make an "instance" of this class via this code:

MyAbstractClass myAbstractClassInstance = new MyAbstractClass() {

    public void myAbstractMethod() {
        // add abstract method implementation here

    }

};

myAbstractClassInstance extends your MyAbstractClass in this case. When you instantiate this class you have to implement all abstract methods as you can see from the code above.

The same way works for interfaces, assume you have an interface MyInterface with a void method myInterfaceMethod inside, then you can create an "instance" (implementation of this instance) via this code:

MyInterface myInterfaceImpl = new MyInterface() {

    public void myInterfaceMethod() {
       // add method implementation here

    }

}

myInterfaceImpl is an implemetation of MyInterface in this case. When you create an object using interface, you have to implement interface methods as it is shown above.

0

You cannot create instances of abstract classes or interfaces using the new operator. For example,

new AbstractSet(); // That's wrong.

You can, however, use them to declare reference variables. For example, You can do this:

AbstractSet set;

You can instantiate anonymous as well as declared implementing classes or subclass.

For example, Set extends AbstractSet, so you can instantiate Set.

Wololo
  • 841
  • 8
  • 20
  • I didn't say this. I mentioned, "You can however you them reference variable's type." Sorry if it confused you. I'll make edits – Wololo Aug 29 '15 at 16:10
0

Interface :

interface Interface1 {

public void m1();

}

When you right

new Interface1() { 

  public void m1() {

   }

}

Its not actually creating the instance of Interface. Its creating an instance of its subtype which doesnt have any name/reference. Hence we cannot create an instance of interface or abstract class

Rahman
  • 3,755
  • 3
  • 26
  • 43
0

Yes, we can create by having defining the abstract methods or the interface methods on the fly during instantiation. That's like a Named anonymous class.

//interface
Runnable r = new Runnable(){
    public void run() {
        System.out.println("Here we go");
    }
};

//Abstract class    
abstract class MyAbstract {
    abstract void run();
}

MyAbstract ab = new MyAbstract(){

    @Override
    void run() {
        System.out.println("Here we go");
    }};
Gouri
  • 347
  • 1
  • 5