Execute Abstract class method without extending it

We can execute abstract class method without extending it in on anther class.

It is possible with anonymous inner class.

Example:

package com.gudla.abstractexample;

public abstract class AbstractExample {
    void test(){
        System.out.println("I am from Abstract Class");
    }
}

package com.gudla.abstractexample;

public class AbstractExampleTest {
    public static void main(String[] args) {
        AbstractExample ae = new AbstractExample(){
        };
        ae.test();
    }
}

Output:

I am from Abstract Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s