Showing posts with label abstract class. Show all posts
Showing posts with label abstract class. Show all posts

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.

Can we create the object of an abstract class ?

  1. abstract public class MyAbstractExe {
  2. MyAbstractExe exe = new MyAbstractExe();
  3. }

No ! we can not create the object / instance of an abstract class. Compiler will give an error at line 2 saying cannot instantiate the type MyAbstractExe 

Can a class inherit from one abstract class ?

  1. public class MyAbstractExe2 extends MyAbstractExe1
  2. {
  3. @Override
  4. void run() {
  5. System.out.println("run");
  6. }
  7. }

  8. abstract class MyAbstractExe3 {
  9. abstract void run();
  10. void test() {
  11. System.out.println("test of MyAbstractExe3 !!");
  12. }
  13. }

  14. abstract class MyAbstractExe1 {
  15. abstract void run();
  16. void test() {
  17. System.out.println("test of MyAbstractExe1 !!");
  18. }
  19. }
Yes! we can inherit a class from one abstract class. compiler will not give error.

Can a class inherit from multiple abstract class ?

  1. public class MyAbstractExe2 extends MyAbstractExe1, MyAbstractExe3
  2. {
  3. @Override
  4. void run() {
  5. System.out.println("run");
  6. }
  7. }

  8. abstract class MyAbstractExe3 {
  9. abstract void run();
  10. void test() {
  11. System.out.println("test of MyAbstractExe3 !!");
  12. }
  13. }

  14. abstract class MyAbstractExe1 {
  15. abstract void run();
  16. void test() {
  17. System.out.println("test of MyAbstractExe1 !!");
  18. }
  19. }
No ! we can not inherit a class from multiple abstract class. Syntax error by compiler at line 1

Can we have an abstract class with both abstract and concrete method ?

  1. abstract class MyAbstractExe {
  2. int x = 10;
  3. abstract void run();
  4. void test() {
  5. System.out.println("yes we can have an abstract class with concrete method !!");
  6. }
  7. }
Yes ! we can have an abstract class with both abstract & concrete method. there is no error.

Can we have an abstract class with concrete method ?

  1. abstract class MyAbstractExe {
  2. int x = 10;
  3. void test() {
  4. System.out.println("yes we can have an abstract class with concrete method !!");
  5. }
  6. }
Yes ! we can have an abstract class with concrete method !! There is no error.

Ca we have an abstract class without abstract method ?

  1. abstract class MyAbstractExe {
  2. int x = 10;
  3. }
Yes ! we can have an abstract class without abstract method. There will not be any error.

Can we have a constructor in an abstract class ?

  1. abstract class MyAbstractExe {
  2. MyAbstractExe() {
  3. System.out.println("yes we can have constructor in abstract class");
  4. }
  5. }
Yes ! we can have a constructor and there is no error.

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...