• Post Reply Bookmark Topic Watch Topic
  • New Topic

a certain bug i cant understand..  RSS feed

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i marked the location of the bug
it says
"implicit super constructor A() is undefined.must explisitly
invoke another constructor"

what does it meen??

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because B extends A, every constructor of B must call a constructor of A using the "super" keyword. If you don't do this yourself, the compiler will automatically add a call to super() -- i.e., it will call A's constructor with no arguments. But since A has no such constructor, this is an error -- you must call one of A's constructors yourself. In other words, B(int) must start with super(X), where X is some integer.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor of a child class must always call a constructor in the parent class. If you don't do this explicitly (using super()), then the compiler will implicitly include a call to the parent's no arg constructor. If the parent doesn't have a no arg constructor, then you will get this error.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have created a constructor in the father called A()
an i have written in my constructor super()

is that what i shoul do in every constructor of a class which extends another class??
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by donaldth smithts:
i have created a constructor in the father called A()
an i have written in my constructor super()

is that what i shoul do in every constructor of a class which extends another class??
Not absolutely necessary to insert a super(); call when there is an accessible no-arguments constructor, but it will do no harm.

But you have a design problem. If your superclass has a field x which you initialise, then all the subclasses will also have to initialise x. So you would have a superclass constructor like thiswhich means that your subclass has to set up x. Yuo do that via a super call, like this:
CR
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the super constructor has a no argument constructor and if that is the one that you are planning to invoke then you need not write
super(); explicitly in the sub class

If you are planning to call a parameterized constructor then an explicit call has to be made as in the code above
super(d);


The main point here is that:
The subclass constructor will invoke the default constructor of the super class always implicitly. If you, want to call the overloaded constructor then an explicit call has to be made.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you say it in simple words??
what are tge rules
if i got in the son some constructor
then in what case i dont need to write super
and when do need to write super???

i have understood that the problem with my code was that
in the father i didnt have any A() type constructor
because of that i need to wright in my son contructor
super(b);
but the problem is that i dont want to call the constructor of A
i dont want this function to change my variables
??
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to call the no arg constructor in your parent class then there is no need to put a call to super() in your child constructor (although it won't hurt if you do). If you want to call a constructor in your parent class that takes arguments then you have to put an explicit call in your child constructor.
If you don't put a call to super() in your child constructor, then your parent class must have a no arg constructor.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by donaldth smithts:
can you say it in simple words??
. . .
the problem is that i dont want to call the constructor of A
i dont want this function to change my variables
??
We have tried to say it simply.

If you don't want to call the superclass constructor, then your subclass ISN'T-A superclass, and you oughtn't to use the extends keyword in the first place.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!