• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Default constructor

 
Ranch Hand
Posts: 1907
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code gives an error as default constructor for B is needed.Why compiler enforces this rule?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default the subclass calls the no arg constructor of the super class.
 
Arjun Shastry
Ranch Hand
Posts: 1907
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic