File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Default constructor" Watch "Default constructor" New topic
Author

Default constructor

Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1874

The above code gives an error as default constructor for B is needed.Why compiler enforces this rule?


MH
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Are you asking why this?

If you create constructors on your own, you will be responsible for the default constructor. The compiler will not create the default constructor for you.


Groovy
saran ram
Ranch Hand

Joined: Dec 13, 2002
Posts: 69
If you don't provide default constructor on your own,
you have to call your superclass constructor in your subclass constructor.
public B(int a,int b)
{
super(a,b);
}
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

By default the subclass calls the no arg constructor of the super class.
Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1874
Originally posted by Pradeep Bhat:

If you create constructors on your own, you will be responsible for the default constructor. The compiler will not create the default constructor for you.

Correct,but what does compiler gain by forcing us to write default constructor?When we don't provide any constructor,compiler creates it for that.So why not in this case too?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Arjun Shastry:

Correct,but what does compiler gain by forcing us to write default constructor?When we don't provide any constructor,compiler creates it for that.So why not in this case too?


If a class defines even one constructor, then the compiler assumes the class designer is taking control of construction and does not create a constructor.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi
what does compiler gain by forcing us to write default constructor?When we don't provide any constructor,compiler creates it for that.So why not in this case too?
When you do not want to invoke a default constructor, your motion seems to want to define another constructor that you want to use and do not want to use the default one. So, the compiler knows what you want to do and do not give the default constructor to you.

In the real live, You can think about that you go out to take a dinner with your friends, if you cliam that you have forgotten to bring your money. Will you friends help you to pay? :roll:
On the other hand, if you have money, you might pay your bill by yourself and your friends might not give a free dinner to you. Right?


Francis Siu
SCJP, MCDBA
Muralidhar Mandapati
Greenhorn

Joined: Nov 14, 2004
Posts: 23
When the object of the subclass is instantiated, superclass would be instantiated with the default constructor.
however when the constructors are overloaded without having a default constructor compiler does not by default provide with the default constructor and hence forces us to provide with the default constructor.

Hence :

class A {
public A(){
}
public A(int a){
}
}
public class B extends A{
public B(int b){
}
public static void main(String[] str){

B b = new B(10);
}
}

could compile while not having the default constructor in the parent class would not compile.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default constructor