CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS J2EE JAVA DETAILS
Question :
Are constructors inherited?Can a subclass call the parent's class constructor?When?
Category Java Interview Questions
Rating (4.0) By 45 users
Added on 10/13/2004
Views 8909
Rate it!
Answers:

You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you
probably don't want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses
constructor you would erode the encapsulation abilities of the language.



constructors are not inherited.You can call the superclass constructor with the help of
super() from derived class.



Posted by: ramya    

Contact ramya Contact ramya

Yes a subclass can call a super class constructors by using the keyword super,by using super it will call the super class which in turn the super class constructors.



Posted by: debraj    

Contact debraj Contact debraj

inheritance always calling constructor in super class.



Posted by: Neeraj sahu    

Contact Neeraj sahu Contact Neeraj sahu

we can not inherit a constructor of super class because constructors can not be overriden.that is we can not insantaniate sub class using super class



Posted by: PRASAD    

Contact PRASAD Contact PRASAD

Constructors is a super class.It is a
using the keyword super call the super class.



Posted by: meenakshi    

Contact meenakshi Contact meenakshi

Constructors is a super class.It is a
using the keyword super call the super class.



Posted by: meenakshi    

Contact meenakshi Contact meenakshi

1:- We can call the super class constructor by the use of super keyword at the first line inside the constructor and giving the parameters to it.

2:- If a subclass contain a no-args constructor(NO argument constructor) then the call to super is done automaticly in the first line of subclass constructor by default and call the super class no -args constructor.

eg:-
class A{
A(){
s.o.p("A");
}
}
class B extends A{
B(){
s.o.p("B");
}
}
class Z{
psvm(String[]args)
{
B b=new B();
}
}
execution- B()->A()->B()
output:- A
B



Posted by: Sourav    

Contact Sourav Contact Sourav

First constructors are not methods...they do not define any functionality. Inheritance is done to reuse the functionality(methods).So even constructors have modifiers private,public,default,protected. If we make the constructor as private then the sub class can not create object with no-arg constructor.



Posted by: mahanth hiremath    

Contact mahanth hiremath Contact mahanth hiremath

Basically constructors are used to create a instance of class and pass some values to member variables, before creating a object of that class. And constructors are not a methods to override the functionality, so the purpose of inheriting the constructor is useless. And by java constructors cannot be inherited they can only be accessed by using the keyword called super.



Posted by: senthil nathan    

Contact senthil nathan Contact senthil nathan

If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    Does Java have destructors?
    View Answer
    What does the "abstract" keyword mean in front of a method? A class?
    View Answer
    Name four methods every Java class will have.
    View Answer
    Given a text file, input.txt, provide the statement required
    View Answer
    Discuss the differences between creating a new class, extending a class and implementing an interface; and when each would be appropriate.
    View Answer
    What's the difference between the == operator and the equals() method? What test does Object.equals() use, and why?
    View Answer
    Why do you create interfaces, and when MUST you use one?
    View Answer
    What is the difference between instanceof and isInstance?
    View Answer
    Are there any other 'marker' interfaces?
    View Answer
    How many methods do u implement if implement the Serializable Interface?
    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL Java Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/385/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

    2.19
    Cache = 0 Seconds