17

How do I create an object of an abstract class and interface? I know we can't instantiate an object of an abstract class directly.

Drise
  • 4,310
  • 5
  • 41
  • 66
gautam
  • 171
  • 1
  • 1
  • 3

12 Answers12

16

You can not instantiate an abstract class or an interface - you can instantiate one of their subclasses/implementers.

Examples of such a thing are typical in the use of Java Collections.

List<String> stringList = new ArrayList<String>();

You are using the interface type List<T> as the type, but the instance itself is an ArrayList<T>.

wkl
  • 77,184
  • 16
  • 165
  • 176
10

To create object of an abstract class just use new just like creating objects of other non abstract classes with just one small difference, as follows:

package com.my.test;

public abstract class MyAbstractClass {
    private String name;

    public MyAbstractClass(String name)
    {
        this.name = name;
    }

    public String getName(){
        return this.name;
    }


}

package com.my.test;

public class MyTestClass {

    public static void main(String [] args)
    {
        MyAbstractClass ABC = new MyAbstractClass("name") {
        };

        System.out.println(ABC.getName());
    }

}

In the same way You can create an object of interface type, just as follows:

package com.my.test;

public interface MyInterface {

    void doSome();
    public abstract void go();

}

package com.my.test;

public class MyTestClass {

    public static void main(String [] args)
    {

        MyInterface myInterface = new MyInterface() {

            @Override
            public void go() {
                System.out.println("Go ...");

            }

            @Override
            public void doSome() {
                System.out.println("Do ...");

            }
        };

        myInterface.doSome();
        myInterface.go();
    }

}
Marcin
  • 125
  • 1
  • 3
  • 19
    That is NOT an object of abstract class or interface. That is an anonymous class extending(implementing) said abstract class(interface). Huge conceptual difference. – Deltharis Sep 22 '14 at 10:59
  • It creates object of the proxy class which implements that interface (or extends the abstract class). Abstract class and interface can not be instantiated. – sohal Nov 15 '18 at 17:09
  • Yes, Deltharis is right, please @Marcin, update your answer to be precise and not misleading people learning java. – Combine Dec 23 '19 at 18:02
5

There are two ways you can achieve this.

1) Either you extend / implement the Abstract class / interface in a new class, create the object of this new class and then use this object as per your need.

2) The Compiler allows you to create anonymous objects of the interfaces in your code.

For eg. ( new Runnable() { ... } );

Hope this helps.

Regards, Mahendra Liya.

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
4
public abstract class Foo { public abstract void foo(); }
public interface Bar { public void bar(); }
public class Winner extends Foo implements Bar {
  @Override public void foo() { }
  @Override public void bar() { }
}
new Winner(); // OK
Community
  • 1
  • 1
maerics
  • 151,642
  • 46
  • 269
  • 291
  • 1
    foo() method in abstract class requires keyword abstract, otherwise it throws compile time error. It should be like, public abstract void foo(); – user609239 Aug 02 '11 at 07:05
4

You can provide an implementation as an anonymous class:

new SomeInterface() {
    public void foo(){
      // an implementation of an interface method
    }
};

Likewise, an anonymous class can extend a parent class instead of implementing an interface (but it can't do both).

Dmitri
  • 8,999
  • 5
  • 36
  • 43
3

"instantiate" means "create an object of".

So you can't create one directly.

The purpose of interfaces and abstract classes is to describe the behaviour of some concrete class that implements the interface or extends the abstract class.

A class that implements an interface can be used by other code that only knows about the interface, which helps you to separate responsibilities, and be clear about what you want from the object. (The calling code will only know that the object can do anything specified in the interface; it will not know about any other methods it has.)

If you are using someone else's code that expects a Fooable (where that is the name of some interface), you are not really being asked for an object of some Fooable class (because there isn't really such a class). You are only being asked for an instance of some class that implements Fooable, i.e. which declares that it can do all the things in that interface. In short, something that "can be Foo'd".

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
2

You can not instantiate the abstract class or an interface, but you can instantiate one of their subclasses/implementers.

You can't instantiate an abstract class or an interface, you can only instantiate one of their derived classes.

In your example

 MyAbstractClass ABC = new MyAbstractClass("name") {
    };

You are instantiating any class that implements Suprising.

Oriol Roma
  • 329
  • 1
  • 5
  • 9
AnonDCX
  • 2,501
  • 2
  • 17
  • 25
  • This is anonymous class which extends the MyAbstractClass and which you declare and instantiate at the same time. – Combine Dec 23 '19 at 18:15
2

You write a class that derives from the abstract class or implements the interface, and then instantiate that.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

What you know is correct. You cannot create an object of abstract class or interface since they are incomplete class (interface is not even considered as a class.)

What you can do is to implement a subclass of abstract class which, of course, must not be abstract. For interface, you must create a class which implement the interface and implement bodies of interface methods.

Here are orginal tutorial on oracle site, http://download.oracle.com/javase/tutorial/java/IandI/abstract.html and http://download.oracle.com/javase/tutorial/java/concepts/interface.html

gigadot
  • 8,879
  • 7
  • 35
  • 51
1

NO, we can't create object out of an interface or Abstract class because

Main intention of creating an object is to utilize the wrapped methods and data. As interface don't have any concrete implementation hence we cannot.

For abstract class we may have concrete method or abstract method or both. There is no way for the API developer to restrict the use of the method thats don't have implementation.

Hope help.

tsingh
  • 21
  • 1
  • you can create a object of interface and abstract class using anonymous class see this http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html – Simmant Jul 11 '14 at 11:06
  • Than it's not an object of interface or abstract class. It's an object of said anonymous class. – Deltharis Sep 22 '14 at 11:20
1
public abstract class AbstractClass { ... }

public interface InterfaceClass { ... }

// This is the concrete class that extends the abstract class above and
// implements the interface above.  You will have to make sure that you implement
// any abstract methods from the AbstractClass and implement all method definitions
// from the InterfaceClass
public class Foo extends AbstractClass implements InterfaceClass { ... }
digiarnie
  • 22,305
  • 31
  • 78
  • 126
0

No, you are not creating the instance of your abstract class here. Rather you are creating an instance of an anonymous subclass of your abstract class. And then you are invoking the method on your abstract class reference pointing to subclass object.