Can we provide the constructor in abstact class? If yes why? what is internal mechanism of jvm

Showing Answers 1 - 8 of 8 Answers

nakt

  • Nov 20th, 2005
 

Why not ? Abstract classes are those classes where we do not want them to be instantiated and possibly all methods are not clear by the time when the abstreact classes are defined. But the abstract classes are used while the classses of the systen are refined, reshaped. The abstract class has the common data which can be constant or need to be set by the subclasses based on the specialisation . Therefore the subclass may want to set the abstracr classes variables as and when the subclass is created . Therefore the constuctor should be used in such a case.The jvm creates first the super class and then subclass on newing of the subclass

Srinivasa Rao Goineni

  • Sep 22nd, 2006
 

Hi,

   We can't make constructor as abstract in an abstract class.Constrctor won't be inherited from base class to subclass.

Thanks

Srinivas gogineni

  Was this answer useful?  Yes

biju

  • Jan 17th, 2007
 

we can provide constructor to abstract class. this is for inheritance (for extended classes). This constructor can be invoked by subclasses. But ?new? cannot be used.

public abstract class ABS{

 public ABS(){
  System.out.println("constructor ABS");
 }

 public abstract void m1();
}

public class ABS1 extends ABS{

 public ABS1(){
  super();
 }

 public void m1(){
 }

 public static void main(String[] args){

  System.out.println("ABS1");
  new ABS1();

 }

}

sampra

  • Feb 13th, 2008
 

  We can't make constructor as abstract in an abstract class.Constrctor won't be inherited from base class to subclass.

  Was this answer useful?  Yes

An abstract class can always have a constructor for a reason that an abstract class even though cannot be instantiated but it can be extended. Once its extended by its subclass. the object of the subclass can be instantiated. Incase of constructors its first calls this() or super() sothe constructor of the base class is called finally till the control reach the Object class constructor. Once the constructor of the Object class completes the control goes to its child class which in its case can be an abstract class. hence further on the control reaches to the constructor of the child class (of the abstract class). hence we can say that there is a need for the constructor of the abstract class

  Was this answer useful?  Yes

Yes, abstract classes may contain constructors reason is:

abstract classes may contain variables to instantiate those variables we use constructors.

  Was this answer useful?  Yes

akash

  • Aug 10th, 2012
 

Any class be it abstract or not, has a default constructor created by the jvm.

  Was this answer useful?  Yes

Rinky Arora

  • Nov 8th, 2017
 

Yes, we can provide constructor in abstract class and if we don't provide JVM will implicitly provide the default constructor in abstract class. The mechanism is, abstract class is nothing more than a normal class but with abstract methods inside it. So, whenever the object of the child class (class which extends abstract class) get created, first object of the abstract class get created by JVM by calling the constructor.
So, behind the scene constructor of abstract class always invoked by the JVM if you provide or not.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions