• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

constructor

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic