GeekInterview.com
Series: Subject: Topic:
Question: 34 of 38

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

Asked by: Interview Candidate | Asked on: Nov 19th, 2005
Showing Answers 1 - 7 of 7 Answers
nakt

Answered On : 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
Srinivasa Rao Goineni

Answered On : 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

  
Login to rate this answer.
biju

Answered On : 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();

 }

}

Yes  3 Users have rated as useful.
  
Login to rate this answer.
sampra

Answered On : Feb 13th, 2008

View all answers by sampra

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

  
Login to rate this answer.
lakshminarang

Answered On : Mar 11th, 2008

View all answers by lakshminarang

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

  
Login to rate this answer.
iamlegend108

Answered On : May 2nd, 2011

View all answers by iamlegend108

Yes, abstract classes may contain constructors reason is:

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

  
Login to rate this answer.
akash

Answered On : Aug 10th, 2012

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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.