• Post Reply Bookmark Topic Watch Topic
  • New Topic

having problem in Inheritance  RSS feed

 
Deepak Monster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am a beginner and have just started learning java. i was trying to run simple inheritance example but having a problem




In 23rd line i am having a red circle which says:

constructor a in class a can not be applied to given types
required : double,double,double
found:no arguments
reason actual and formal arguments differ in range


Please help

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default the constructor of the sub class adds a super(); call to the constructor of the Super class. In you case as you have overloaded the constructor of class a, you need to provide the default constructor in class a.
OR
you can explicitly call super(val1,val2,val3); in the constructor of class p.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a constructor doesn't explicitly call this(...) or super(...) as its first statement, the compiler inserts a call to the no-arg parent class c'tor, super(). You parent class doesn't have such a c'tor. So you need to either add one, or, what seems a better approach here, call the one that does exist.

And once you do that, you won't need to initialize 3 of those variable in the child's c'tor since the parent is doing that for you.

In the future, please use meaningful names and follow standard Java coding conventions (such as class names starting with an uppercase letter). This will make it easier to read and understand your code, wihch will make it easier for people to help you.

Also, you should almost always make your member variables private. Subclasses and other classes shouldn't have direct access to them.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in reading a related discussion here
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
In the future, please use meaningful names and follow standard Java coding conventions (such as class names starting with an uppercase letter). This will make it easier to read and understand your code, wihch will make it easier for people to help you.


I found a few naming conventions here. Also there is a Style guide compiled by JavaRanch
 
Deepak Monster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THank you very much guys. i added a default constructor for super class and it worked .Thanks a lot
and Mohamed Sanaullla sir, i will definatly take care of the naming conventions next time.
Thanks a lot again
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Monster wrote: . . . i added a default constructor for super class and it worked . . .
No, you didn’t. You added a no-arguments constructor. Only the compiler ever adds a default constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!