16
abstract class A {
    public void disp() {
        System.out.print("Abstract");
    }
}

public class B {
    public static void main(String args[]) {
        A object = new A(){ };
        object.disp();
    }
} 

I am aware that Abstract classes cannot be instantiated, but confused on this code. Actually what this code mean ?

2 Answers2

30

The subtlety here is in the "{}". It means you explicitly provide an anonymous implementation for the missing parts (the missing parts are abstract methods) of the abstract class A allowing you to instantiate it.

But there's no abstract method in A, therefore the anonymous implementation is empty.

Example showing the behaviour with at least one abstract method:

public abstract class A {
    public abstract void bar();
    public void disp() { System.out.print("Abstract"); } 
}

public class B { 
    public static void main(String args[]) { 
        A object = new A() { 
            @Override public void bar() { System.out.print("bar"); } 
        }; 
        object.disp(); //prints "Abstract" 
        object.bar(); //prints "bar"
    } 
} 
Spotted
  • 4,021
  • 17
  • 33
  • I can’t decide if this is a “cool” thing Java has that C#’ does not; to a workaround for java not originally have delegates and events. – Ian Ringrose Sep 02 '15 at 11:41
  • Is there a reason to declare class A as abstract, even though you haven't got any abstract methods in it (as in the OP question's example)? Why not just remove the "abstract" qualifier from the declaration of class A. – Brandin Sep 02 '15 at 12:19
  • @Brandin - perhaps you want a base class with a few non-abstract methods that should never be instantiated? Like, a `Shape` class, that is inherited by `Triangle`, `Square` and so on, and has a shared method `getLocation()`, but you don't ever want someone to make an instance of `Shape`. It exists only for inheritance. – Davor Sep 02 '15 at 12:25
  • @Brandin for me it also doesn't make sense to have an abstract class without abstract method. Having such a class (like `Shape` proposed by Davor) is in contradiction with the OOP philosophy. – Spotted Sep 02 '15 at 12:34
  • @Brandin - hey, I'm not saying that this is smart. Just proposing a possible reason. – Davor Sep 02 '15 at 13:00
  • @Spotted - I assume you're a C++ programmer? This is the case in C++ because there is no such thing as an abstract class without a pure virtual function - that's in fact the only way to make a class abstract. Languages like Java or C# have the `abstract` keyword to explicitly state that a class is abstract, whether or not it has something that needs to be overridden in a subclass. – Darrel Hoffman Sep 02 '15 at 13:39
  • @DarrelHoffman nope, I'm a Java programmer :-) and I'm sad that Java (or C#) allows an abstract class without abstract methods. – Spotted Sep 02 '15 at 13:52
14

This is called an anonymous inner class. You are not instantiating the abstract class, you are instantiating the concrete anonymous inner class which extends the abstract class. Of course, in order for this to be allowed, the anonymous inner class must provide implementations for all the abstract members of the abstract superclass … which it does in this case, because the abstract superclass has no abstract members.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653