DroidNinja
DroidNinja

Reputation: 1007

Creating the instance of abstract class or anonymous class

In this code here is it creating the object of abstract class or anonymous class? Please tell me. I am little bit confused here.

public abstract class AbstractDemo {

    abstract void showMessage();
    abstract int add(int x,int y);

    public int mul(int x,int y){
        return x+y;
    }


    public static void main(String[] args) {
        AbstractDemo ad = new AbstractDemo() {

            @Override
            void showMessage() {
                // TODO Auto-generated method stub

            }

            @Override
            int add(int x, int y) {
                // TODO Auto-generated method stub
                return 0;
            }
        };

        System.out.println(ad.mul(10, 12));
        System.out.println(ad.getClass());
    }

}

Upvotes: 18

Views: 45673

Answers (8)

Vincent van der Weele
Vincent van der Weele

Reputation: 13177

You create an anonymous class that extends your abstract class.

In the snippet below, you are extending AbstractDemo and providing implementations for its abstract methods.

new AbstractDemo() {
    @Override
    void showMessage() {
        // TODO Auto-generated method stub
    }

    @Override
    int add(int x, int y) {
        // TODO Auto-generated method stub
        return 0;
    }
};

Upvotes: 37

Piotr Sobczyk
Piotr Sobczyk

Reputation: 6583

Here's what happened in this short and innocent piece of code:

AbstractDemo ad = new AbstractDemo() {

            @Override
            void showMessage() {
                // TODO Auto-generated method stub

            }

            @Override
            int add(int x, int y) {
                // TODO Auto-generated method stub
                return 0;
            }
        };
  1. New class was defined (without a name, so called anonymous class)
  2. This new class extends AbstractDemo class
  3. Abstract methods of AbstractDemo were overridden in this new class
  4. New instance of this new class was created and assigned to ad variable

Read more about anonymous classes in Java here.

Upvotes: 11

praveen Babu
praveen Babu

Reputation: 7

@Override // Here
void showMessage() {
    // TODO Auto-generated method stub
}

@Override // here
int add(int x, int y) {
    // TODO Auto-generated method stub
    return 0;
}

first of all you have to know that you can't create an instance for abstract class, here you are just creating a anonymous class which is act like a inner class that extends the abstract class so its belongs to your anonymous class.

Upvotes: 1

Gaurav Tiwari
Gaurav Tiwari

Reputation: 1

Here we are providing the implementation of abstract class.By the time we are making object ,we are providing its implementation and overriding all abstract methods.In the case of Abstract class having only concrete method. See below Example

abstract class MyAbstractClass {
    private String name;

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

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

public class Test {

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

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

Here We are just making object using invoking constructor like {} .

  1. we are proving implementation using anonymous class.

2.Which is nothing but class without name which we need to do as an when we make an object.

  1. Yes,we are making an object of abstract class by proving the implemention at runtime.

4.It open up the answer like why abstract class allow constructor ? but we can not create an object of it.

5.another question related to abstract class is we can have only static block in abstract class.what is this meaning?

6.Only static block because we want to load one time only without extending and creating object of it.

7.After compiling code we use to get .class of every concrete class,abstract class or interface.

8.Next question arises here Why java folks donot allow static block in interface?

Upvotes: 0

anshulkatta
anshulkatta

Reputation: 2064

A standalone Abstract class's Object can't be made but an anonymous abstract class object can be made because it is providing implementation then and there.

The reason why Java doesn't allow object of abstract class is , because it doesn't have any implementation for any method or for some of the methods , so how could you have an object of incomplete class. But in Anonymous way here you are giving it implementation so it is allowing you to have an object.

Same case with interface

 AbstractDemo ad = new AbstractDemo() {

            @Override
            void showMessage() {
                // TODO Auto-generated method stub

            }

            @Override
            int add(int x, int y) {
                // TODO Auto-generated method stub
                return 0;
            }
        };

Here AbstractDemo class is abstract , but its implementation class can have the object so , here anonymous class code is implementing the code , hence that is perfectly allowed to have object.

More digging into it , see the code below , MyAbstractClass is an abstract class , now if you comment out

abstract void play();

then it works fine , Java is allowing to make object of this abstract class but when you uncomment that like ,it denies because it doesn't have any implementation about that method , so denies to make object.

abstract class MyAbstractClass {
    private String name;

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

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



}


public class Test2 {

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

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

}

check carefully it is {} after the constructor calling , that means no more implementation required , or you can override its one of method or more.

Upvotes: 0

amicngh
amicngh

Reputation: 7899

You are overriding abstract methods in anonymous class..that's why you can create the object. see below.

        @Override // Here
        void showMessage() {
            // TODO Auto-generated method stub

        }

        @Override // here
        int add(int x, int y) {
            // TODO Auto-generated method stub
            return 0;

An implementation of Abstract Class can be instantiated as you are doing.

Upvotes: 0

Shreyos Adikari
Shreyos Adikari

Reputation: 12744

You can not create an instance of an abstract class.
You can create an instance of a class that extents your abstract class.

The whole point of an abstract class is that it's abstract -- you've defined an interface but not an implementation. Without an implementation, instantiating the class wouldn't produce a meaningful or useful result. If it does/would make sense to instantiate objects of that class, then you simply don't want to use an abstract class in the first place.

You can use anonymous class concept for an instance like the below:

AbstractDemo abstractDemo  = new AbstractDemo() {
            @Override
            void showMessage() {
                // TODO Auto-generated method stub
            }
            @Override
            int add(int x, int y) {
                // TODO Auto-generated method stub
                return 0;
            }
        }; 

Upvotes: 5

sadhu
sadhu

Reputation: 1479

Your AbstractDemo is no more abstract after its abstract methods are implemented in the anonymous class. Its as good as saying:

Class AbstractDemoImpl extends AbstractDemo {

    void showMessage() {
            // TODO Auto-generated method stub
    }
    int add(int x, int y) {
            // TODO Auto-generated method stub
            return 0;
    }
}

Upvotes: 0

Related Questions