Monday, April 23, 2018

Is there any way to create the instance of an abstract class ?

  1. public class MyMain{
  2. public static void main(String[] args) { // start
  3. MyAbstractClass exe = new MyAbstractClass() {
  4. @Override
  5. void run() {
  6. System.out.println("run of abstract class");
  7. }
  8. };
  9. System.out.println(exe.x);
  10. exe.run();
  11. } // end
  12. }

  13. abstract class MyAbstractClass {
  14. int x = 10;
  15. abstract void run();
  16. }
Output: 
10
run of abstract class

Note: Yes ! using anonymous class we can create the instance of an abstract class.

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...