- 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???
- package com.instanceofjava.abstractclassconstructor;
- public abstract class AbstractDemo {
- AbstractDemo(){
- System.out.println("No argument constructor of abstract class");
- }
- }
- package com.instanceofjava.abstractclassconstructor;
- public class Test extends AbstractDemo{
- Test(){
- System.out.println("Test class constructor");
- }
- public static void main(String[] args) {
- Test obj = new Test();
- }
- }
Output:
- No argument constructor of abstract class
- 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?
- package com.instanceofjava.abstractclassconstructor;
- public abstract class AbstractDemo {
- AbstractDemo( int x){
- System.out.println("No argument constructor of abstract class x="+x);
- }
- }
- package com.instanceofjava.abstractclassconstructor;
- public class Test extends AbstractDemo{
- Test(){
- super(10);
- System.out.println("Test class constructor");
- }
- public static void main(String[] args) {
- Test obj = new Test();
- }
- }
Output:
- No argument constructor of abstract class x=10
- Test class constructor
Program #3: What will happen if we are not placing super call in sub class constructor?
No comments