ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
Can you call a constructor within a constructor?
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Can you call a constructor within a constructor?
Answer
# 1
Yes once constructor can call another constructor by using
the this() statement. Example:

class Account {
    private double balance;
    
    // 1st Constructor
    public Account( double initBalance ) {
        balance = initBalance;
    }

    // 2nd Constructor
    public Account() {
        this( 0.0 );    // call the 1st constructor
    }
}
 
Is This Answer Correct ?    1 Yes 0 No
Ranganathkini
 
  Re: Can you call a constructor within a constructor?
Answer
# 2
And to call the constructor of some other class, use super()
 
Is This Answer Correct ?    0 Yes 0 No
Harpreet
 
 
 
  Re: Can you call a constructor within a constructor?
Answer
# 3
We can call one of  base class constructor inside derive class constructor by using predefine variable "super()" such as super(a), super(a,b) and so on.
 
Is This Answer Correct ?    1 Yes 0 No
Nikhlesh Gupta Datia(m.p.)
 
  Re: Can you call a constructor within a constructor?
Answer
# 4
when i am tring above code getting following error
Can we call a constructor within a constructor?

Error:------------>
object.cpp: In constructor Account::Account():
object.cpp:15: error: this cannot be used as a function


#include<iostream.h>
class Account {
    private:
         double balance;

    // 1st Constructor
    public:
         Account( double initBalance ) {
                balance = initBalance;
        }

    // 2nd Constructor
    public:
        Account() {
        this( 0.0 );    // call the 1st constructor
        }
};

int main()
{
        Account a;
        return 0;
}

Error:------------>
object.cpp: In constructor Account::Account():
object.cpp:15: error: this cannot be used as a function
 
Is This Answer Correct ?    0 Yes 0 No
Jinendra Pandey
 
  Re: Can you call a constructor within a constructor?
Answer
# 5
yes we can call by make use of this()
 
Is This Answer Correct ?    0 Yes 1 No
Ravikiran(aptech Mumbai)
 
  Re: Can you call a constructor within a constructor?
Answer
# 6
yes we can call by super() or this();

public abstract class Class1{
	
	public Class1(){
		System.out.println("Class1");
	}
}

public class Class2 extends Class1{
	
	public Class2(String Name){
		System.out.println("Name = " + Name);
	}
	
	public Class2(){
		this("AMIT"); // use only one this or super
		super();
	}
}
 
Is This Answer Correct ?    0 Yes 0 No
Amit Kumar Gupta
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
What is the catch or declare rule for method declarations?  1
What is method Overloading in the perspective of OOPS?  4
What is meant by class loader and how many types are there? Apple1
What modifiers are used with a top level class?  2
How to create an instance of a class if the class has private constructor?  2
What do you mean by Remote procedure call?  2
How does serialization work Ordain-Solutions2
Can an unreachable object become reachable again?  3
What is Ideal Design of Class?  2
What happens when a main method is declared as private? Sun-Microsystems8
What is more advisable to create a thread, by implementing a Runnable interface or by extending Thread class?  5
diff between abstract methods and interfaces with programing (code) example?  2
Difference between a Scrollbar and a ScrollPane?  1
When is an object in the mean to garbage collection?  3
What is the difference between jsp and servlet? Symphony5
Why const and goto are reserved keyword in java?  1
Name the method that is used to set a TextComponent to the read-only state?  1
How can we find size of the object ? RazorSight1
how to transactions(Bank transactions) in business process in ejb? Photon1
How the elements are organized in CardLayout?  5
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com