Author | Topic: constructor |
Umesh ranch hand |
posted March 25, 2000 10:28 PM
Question 11 in Marcus 3: What will happen when you attempt to compile and run the following code
1) Compile time error Base is a keyword 2) Compilation and no output at runtime //answer 3) Output of Base 4) Runtime error Base has no valid constructor My thinking as follows: The subclass will have default no-arg contructor, which will try calling super().
|
Alkesh Nandanwar ranch hand |
posted March 25, 2000 10:43 PM
hi As Marcus said time to time that his third Mock Test is still not official, you will see lot of mistakes in the exam Alkesh
|
Rolf Weasel ranch hand |
posted March 25, 2000 11:48 PM
Umesh, i think the line causing your confusion is: public void Base(){ This is not a constructor since it defines a return type of void. Hence, there are no constructors defined for the class Base. The compiler therefore supplies a default no-argument constructor, which is invoked by the default no-argument constructor of class In.
|
Umesh ranch hand |
posted March 25, 2000 11:56 PM
Rolf, I don't think class Base will provide no-arg contructor when it has got method(ie. void Base()). I think I need to agree with Alkesh!!!!.
|
Rolf Weasel ranch hand |
posted March 26, 2000 12:00 AM
when u have doubts like that, there's only one way to know for sure...compile and run. the code works fine. check it out for yourself. alkesh, marcus mock 3 may be beta, but it's still way more accurate than most other tests out there . if u encountered any mistakes, bring them up in the Mock Exam Errata page so they can be discussed in full. [This message has been edited by Rolf Weasel (edited March 26, 2000).]
|
sree ranch hand |
posted March 27, 2000 10:34 AM
Since there are no constructors defined in class Base java will automatically provide a no-arg constructor for Base class which doesn't contain anything. In the "In" class the constructor In doesn't contain anything either but a call to super class(Base) constructor will be placed implicitly i.e super().Since both the constructors(Base & In) don't contain anything it will compile and execute fine without giving any output.
|
maha anna bartender |
posted March 27, 2000 11:59 AM
Umesh, The qstn is a good qstn. Also it will compile and run perfectly. And the answer given is also correct. The important points to remember here is 1. Any method/var within the same class can have the same name as the class 2. The difference between a method having the same name as the class and the constructor of the class is the return type. Methods MUST have return type and constructors MUST not have return type in their corresponding declarations. 3. The compiler automatically provides a default (no-arg) constructor if and only if there is no other constructors defined by you. If you decide to provide a constructor taking some args then you HAVE TO define the no-arg constructor. Compiler WILL not give one in this later case. 4. Defining a method with the same name as the class is NO WAY affects the decision made by the compiler whether to provide/not to provide no-arg constructor. So the correct answer for the above qstn is 2) regds [This message has been edited by maha anna (edited March 27, 2000).]
|
Umesh ranch hand |
posted March 27, 2000 11:22 PM
Maha Anna, I agree with all your above points. May be I need to correct my thinking edge....Please help me
|
sree ranch hand |
posted March 28, 2000 09:44 AM
Methods and Constructors are different. Methods return value and constructors won't. There is no connection between methods and constructors. In the above case the method won't stop compiler providing a default no-arg constructor for the class Base.As long as your class doesn't contain any explicitly defined constructors with arg's compiler will automatically provide one default no-arg constructor for your class. CODE: Finally since both the constructors Base(){} and In(){} doesn't contain anything it will compile just fine but without giving any output. I hope you may find this helpful.
|
maha anna bartender |
posted March 28, 2000 10:47 AM
Umesh, Sree has explained well. You see the foll lines carefully. public void Base() {} //It is NOT a constructor. It is a METHOD So the compiler provides the default Base() {} constructor public Base() {} //It IS constructor
|
Umesh ranch hand |
posted March 29, 2000 09:35 PM
WOW.... I messed up with Constructors and Methods here. I really have high regards from bottom of my heart to Maha Anna and Sree. Keep it up guys.
|
Sushma greenhorn |
posted April 05, 2000 03:15 PM
I was wondering if u can have something like this::: class B{ public B(){} } I mean can the access modifier be different for a class and constructor??? Regards, Sushma
|
maha anna bartender |
posted April 05, 2000 04:30 PM
Certainly. The constructors can have any access level. Java doesn't rescrict that. But, a class which has package (default) access level, declaring their constructors public doesn't achieve anything. In order to instantiate a class you first should have the access level of the class. Then comes the access level of the constructors. The given class by you is accessible ONLY by classes in the default package. Only these other classes in the SAME package can make an instance of this class. So both 'public' and 'default' and 'protected' level accesses for the constructors of your class will have the SAME effect when the top level class is inside a package. But if you put 'private', then it has different effect. You can't create an instance of the class from ANY OTHER class. The constructor is known to ONLY ALL MEMBERS INSIDE this class. Still any of the methods inside this class MAY choose to create an instance and give it to ouside world by means of accessor methods. But that happens only if the class wants to. regds maha anna
|
| | |