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