GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 143 of 923    Print  
How are this() and super() used with constructors
this() is used to invoke a constructor of the same class. super() is used to invoke asuperclass constructor.


  
Total Answers and Comments: 1 Last Update: June 16, 2008   
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
June 15, 2008 09:11:09   #1  
vegetto Member Since: June 2008   Contribution: 10    

RE: How are this() and super() used with constructors
this() is used to specify the current class or the child's class constructor always
and super() refers to the parent's class constructor

To use them follw the rules:

super()has to be the first line in any case if you are using the parent's class constructor in the child's class because your code might be using the parent's class variable and usng super() in the midway doesn't make any sense.So it has to be the first line always in the child's constructor.you can also supply super with arguments if ur parent class  has defined such constructor

this() also has to be the first line and is generally used when u r using the concept of constructor overloading then suppose u have 2 constructors eg:

public class Manager extends Employee
{ private String department;
   
     public Manager(String name,double salary)
    {
       super(name,salary); // always first line
         
    }
    public Manager(String name,String dept)
    {
        this (name,dept) // always first line and this refers to above const.
         department=dept;
    }
}
 use this() and super() as shown above
and note that whatever happens the constructor of the parent class will always be executed first and then child's class 

 
Is this answer useful? Yes | No

 Related Questions

A class does not inherit constructors from any of its superclasses. 
Latest Answer :  yes, the super class inherit the construtor. we can call the super class by            using the SUPER keyword. ...

The compiler supplies a default constructor for a class if no other constructors areprovided. 

this() is used to invoke a constructor of the same class. super() is used to invoke asuperclass constructor. 
Latest Answer : this() is used to specify the current class or the child's class constructor alwaysand super() refers to the parent's class constructorTo use them follw the rules:super()has to be the first line in any case if you are using the parent's class ...

this is used to refer to the current object instance. super is used to refer to the variablesand methods of the superclass of the current object instance. 

Answered by Scott on 2005-05-12 09:44:53: Overloading: The same method name with a different signature (generally input argument list) written in the same class.  Overriding: The identical method 
Latest Answer : Overloading:Methods or functions  having same method or function name but have different no. of arguements or different types of arguements. Overriding: Method or functions having same method r function name,type and no. of arguments are ...

A class does not inherit constructors from any of its superclasses. 
Latest Answer : No, Constructors and Destructors are NOT inherited. They are implicitly called by the java compiler. ...

The compiler supplies a default constructor for a class if no other constructors areprovided. 
Latest Answer : If there is no other constructor i.e, if user didn't defined the constructor default constructor will be created and called up ...

this() is used to invoke a constructor of the same class. super() is used to invoke asuperclass constructor. 

this is used to refer to the current object instance. super is used to refer to the variablesand methods of thesuperclass of the current object instance. 

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 
Latest Answer : no,the constructors cannot be inherited.the main reason for that is that we dont want to override the superclass constructor.Yes, asubclass can call its parent's class constructor.This is possible by using SUPER keyword in java.Through this keyword we ...


 Sponsored Links

 
Related Articles

Service Oriented Java Business Integration Review

Service Oriented Java Business Integration Review Introduction If you ve read through the texts which give you an introduction to SOA or Web Services you will often find them to be quite frustrating and the reason for this is because they spend too much time referencing business processes which are
 

Java and Client Server Models

Java and Client Server Models The Role of Client Servers on the Web Client server models provide the essential mechanisms for working with the Internet In fact most of the World Wide Web is built according to this paradigm In client server models the web browsers run by millions of users are the cli
 

The Interview Snafu

How to turn someone else’s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

Winning a Job Interview with a Winning Resume

Does your resume unlock your potential, take your skills to the highest level and win you the interview and the job you want now? The job market today is highly competitive and even if you think you have what it takes to get an interview you won’t get over the line without a polished, prof
 

Using UML with Java

Using UML with Java While Java is not a new language its application for the development of embedded systems is quite new Developers are beginning to take a second look at modeling languages such as UML and many feel it can be a powerful tool in their development arsenal mosgoogle center Introductio
 

Importance of Proper English during Job Interview

Importance of Proper English during Job Interview Your job interview is crucially important and it will determine whether or not you will get the job Depending on the type of job you re going for it is very important for you to use proper English In most cases jobs which offer higher salaries will h
 

Java Technology Trends

Technology Trends Involving Java Java is an object oriented programming language OOP for Web browsers It is organized around data rather than actions and supports polymorphism which allows the same code to be written generically so it can function with different datatypes inheritance which allows on
 

HR Interview - HR Interview Mistakes You Will Want To Avoid

HR Interview Mistakes You Will Want To Avoid The job interview can be a stressful process This is especially true for those who are going after a competitive position Your nonverbal communication combined with the answers you give during the interview will determine if you are hired mosgoogle While
 

HR Interview - Behavioral HR Interviews

Behavioral HR Interviews As the name implies a behavioral interview is an interview that is held by a human resources department to determine if an applicant has the behaviors that are appropriate for a job The company must know how an applicant will behave in a certain situations mosgoogle The logi
 

HR Interview - How To Prepare For Your HR Interview

How To Prepare For Your HR Interview Before you begin thinking about how you are going to dress for the interview it is important to do your research first You should learn everything you can about the company you wish to work for When you have detailed information about your employer you will conve
 

Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape