• Yes we can define a constructor in abstract class in java.

  • Then next question will come like when we can not create object of abstract class then why to define constructor for abstract class.
  • It is not possible to create object of abstract class directly but we can create object of abstract class from sub class which is actually extending abstract class.
  • When we define a abstract class a class must extend that abstract class then only there will be use of that class
  • Then when we create object of class which extends abstract class constructor of sub class will be called from that abstract class constructor will be called and memory will be created for all non static members.
  • If we are not defining any constructor default constructor will be executed.
  • So we can define any number of constructor in abstract class.
  • And it is recommended to define constructor as protected. Because there is only one scenario which we can create object is from subclass so define abstract class constructor as protected always.

Order of  execution of  constructor in Abstract class and  its sub class.

  •  When we  create object of  class which is extending abstract class then it will call abstract class constructor through sub class constructor.
  • Lest see a java example program on abstract class constructor in java

Program #1: Does abstract class have constructor???

  1. package com.instanceofjava.abstractclassconstructor;
  2. public  abstract class AbstractDemo {
  3.  
  4. AbstractDemo(){
  5.         System.out.println("No argument constructor of abstract class");
  6.  }
  7.  
  8. }


  1. package com.instanceofjava.abstractclassconstructor;
  2. public class Test extends AbstractDemo{
  3.  
  4.     Test(){
  5.         System.out.println("Test class constructor");
  6.     }
  7.     
  8. public static void main(String[] args) {
  9.         Test obj = new Test();
  10.        
  11.  
  12. }
  13.  
  14. }


Output: 

  1. No argument constructor of abstract class
  2. Test class constructor

Can we define parameterized constructor in abstract class?

  • Yes we can define parameterized constructor in abstract class.
  • But we need to make sure that the class which is extending abstract class have a constructor and it should call super class parameterized constructor
  • We can call super class parameterized constructor in sub class by using super() call
  • For example: Super(2) ;
  • What will happen if we are not placing super call in sub class constructor?
  • Compiler time error will come.

Program #2: Can we define parameterized constructor in abstract class in java?

  1. package com.instanceofjava.abstractclassconstructor;
  2. public abstract class AbstractDemo {
  3.  
  4. AbstractDemo( int x){
  5.          System.out.println("No argument constructor of abstract class x="+x);
  6.  }
  7.  
  8. }


  1. package com.instanceofjava.abstractclassconstructor;
  2. public class Test extends AbstractDemo{
  3.  
  4.     Test(){
  5.         super(10);
  6.         System.out.println("Test class constructor");
  7.     }
  8.     
  9. public static void main(String[] args) {
  10.         Test obj = new Test();
  11.        
  12.  
  13. }
  14.  
  15. }


Output:
 
  1. No argument constructor of abstract class  x=10
  2. Test class constructor

Program #3: What will happen if we are not placing super call in sub class constructor?


does abstract class have constructor in java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu