• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

[constructor]...

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
int x = 0;
A(int w){
x = w;
}
}
class B extends A{
int x = 0;
B(int w){
x = w +1;
}
}
What's wrong with the code???
---------------------------------
thanks for help, Liao
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, there is no no-argument constructor in the superclass A, as a result, when the implicit call to super() from constructor in class B happens, there will be a compile time error.
Hope it helps.
Guoqiao

Originally posted by chao-long liao:
class A{
int x = 0;
A(int w){
x = w;
}
}
class B extends A{
int x = 0;
B(int w){
x = w +1;
}
}
What's wrong with the code???


 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
that's because the constructor B(int w) invokes super() implicitly. & after u define A(int w) only, there'll be no constructor of A with no arguments. this causes a compile time error.
if u change the code a little, it will be OK.
class A{
int x = 0;
A (int w){
x = w;
}
}
class B extends A{
int x = 0;
B(int w){
super(w);
x = w +1;
}
}
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic