• 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 required??

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when we don't define a constructor it works well as it inherits one from the Object class,but if i define a constructor that takes argument, one in super class and other in sub class without defining a default constructor it gives error saying default not declared. why does this happen when we are actually not calling a default constructor but a constructor with parameter.
to make it more clear:
i have:
class a
{
int i;
a(int x)
{
i=x;
}
}
class b extends a
{
int i;
b(int s)
{
s=i;
}
}
class test
{
public static void main(String we[])
{
a aa= new aa(23);
b bb = new bb(34);
}
}
this program will not compile and generate error!
please trow light on it.


------------------
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, constrictors are not inherited. If you do not create any constrictors for a class, Java will automatically create an empty default constrictor for you. If you create constrictor(s) that takes arguements, no default constrictor is created and if you want one you have to create it yourself.
Now when you create a sub-class and then create a sub-class object, before that object can be created, an object of the super class has to be created first. To do this Java will do allow several things:
1) If the constrictor in the sub-class doesn't explicatly call a constrictor of the super class as the first call in the constrictor, it will call the super class's default constrictor.
2) If the constrictor in the sub-class makes a call to a constrictor in the super class, Java will use that constrictor to create the super class object.
If the subclass constrictor doesn't make a specific call to a constrictor in the super class an there isn't a default constrictor a compile time error is thrown. And this is what you are seeing.
You can correct this in one of 2 ways
1) create a default constrictor in a

2) make an explicate call to the super class constrictor from b

Hope this helps

[This message has been edited by Carl Trusiak (edited September 13, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic