2

When I create the object of an Abstract class, I have to do like this just like an interface.

AbstractClass abstractClass = new AbstractClass() {

          @Override
          public void abstractMethod() {
          }
        };

Does this mean that the object of AbstractClass is an anonymous inner class object?

Mawia
  • 4,220
  • 13
  • 40
  • 55

6 Answers6

2
AbstractClass abstractClass = new AbstractClass() {

          @Override
          public void abstractMethod() {
          }
        };

This block of code means that you are creating an anonymous class which extends AbstractClass. You can also use the same notation for an interface also.

SomeInterface someImplementingClass = new SomeInterface(){/*some code here*/};

This means you are creating a class that implements SomeInterface.

Note that there are certain limitation when you are creating an anonymous class. As and anonymous class is already extending the parent type, you cannot make it extend another class as in java you can extend only on class.

This code will help understand concept of overriding methods in anonymous classes

class Anonymous {
    public void someMethod(){
        System.out.println("This is from Anonymous");   
    }
}

class TestAnonymous{
    // this is the reference of superclass
    Anonymous a = new Anonymous(){ // anonymous class definition starts here
        public void someMethod(){
            System.out.println("This is in the subclass of Anonymous");
        }
        public void anotherMethod(){
            System.out.println("This is in the another method from subclass that is not in suprerclass");
        }
    }; // and class ends here
    public static void main(String [] args){
        TestAnonymous ta = new TestAnonymous();
        ta.a.someMethod();
    //  ta.a.anotherMethod(); commented because this does not compile
    // for the obvious reason that we are using the superclass reference and it 
    // cannot access the method in the subclass that is not in superclass
    }

}

This outputs

This is in the subclass of Anonymous

Remember that anotherMethod is implemented in implemented in the subclass that is created as anonymous class. and a is the reference variable of type Anonymous i.e. superclass of the anonymous class. So the statement ta.a.anotherMethod(); gives compiler error as anotherMethod() is not available in Anonymous.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
1

An object is not a class object (in this context). It is derived from a class. In Java there is a difference between classes and objects, as compared to e.g. prototype based languages (e.g. JavaScript) where this difference does not exist.

In your example you create an anonymous class, create an object of that anonymous class and assign it to a variable; all in one step.

Anonymous classes are always inner classes: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9.5 http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50
0

One fundamental properties of attract classes is the fact that there could be no direct instance of this type. Only classes that implement the complete interface of an class can be instantiated. In order to create an object you need a non abstract class first, by extending the abstract class.

Jan Henke
  • 875
  • 1
  • 15
  • 28
0

Abstract classes does not have any instances (objects of it's type). I recommend Mavia to take a look at following link for clearness: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

primehunter
  • 280
  • 4
  • 10
0

In fact here you create both: an anonymous inner class, that extends AbstractClass as well as an instance of this anonyomous class, the object. You do not and cannot create an instance of AbstractClass.

Also you declare a variable named abstractClass that has the Type AbstractClass. Inside this variable you store a newly created instance of your newly defined subclass of AbstractClass.

EDIT: You can of course not reuse the anonymous inner class, since it is anonymous, and the only place where an instance of it can be created or rather is created is right here.

Here might be a loop or function in which case you would be able to create many instances of this anonymous inner class. But it would still be only this piece of code where instances are created.

John Smith
  • 2,282
  • 1
  • 14
  • 22
0

You cannot create an object of an abstract class. They are non-instantiable. What you are doing when you do this is creating a sort of dynamic subclass object, and instantiating that (at the same time). More or less, yes, you can create it the same way as an interface. See this answer for more information.

Community
  • 1
  • 1
Gurgadurgen
  • 408
  • 3
  • 13