wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor" Watch "constructor" New topic
Author

constructor

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Question 11 in Marcus 3:
What will happen when you attempt to compile and run the following code
<pre>
class Base{
public void Base(){
System.out.println("Base");
}
}
public class In extends Base{
public static void main(String argv[]){
In i = new In();
}
}
</pre>
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().
<pre>
in(){
super()
}
<pre>
why this code will not give compilation error ?
Little fine tuning will help me.....thanx

Nilesh Parikh
Ranch Hand

Joined: Mar 14, 2000
Posts: 52
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

Joined: Feb 26, 2000
Posts: 82
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.


May the Moose be with you.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Feb 26, 2000
Posts: 82
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).]
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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
maha anna
[This message has been edited by maha anna (edited March 27, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Maha Anna,
I agree with all your above points.
According to your point 3....my posting(question) should give compilation error, right ?
Because the Base class will not provide no-arg constructor since it has got method. The subclass In() will look for no-arg constructor in the super!!!
May be I need to correct my thinking edge....Please help me
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
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:
class Base{
public void Base(){
System.out.println("Base");
}
//public base(){} This constructor will be provided by compiler since Base doesn't contain any explicitly defined constructors.
}
public class In extends Base{
public static void main(String argv[]){
In i = new In();
//In() {super();} Same here.This constructor will be provided by compiler since In doesn't contain any explicitly defined constructors.
}
}
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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

regds
maha anna
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Ranch Hand

Joined: Feb 24, 2000
Posts: 36
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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: constructor
 
Similar Threads
Overriding,Overloading - Question
Default constructor !!!!
Marcus Green's Tutorial
Question about Constructors on Marcus Green's Mock Exam #3
Marcus Green Mock exam Question # 16