• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




cannot find symbol
symbol : constructor Inherit()
location: class Inherit
public InheritB(int a,int b,int c) {


Why do I get this error?
thank you
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will work:



Why do I get this error?


you will have to define a call to super explicitly. Compiler is trying to use super(); in your case. But you super class does not have non parametrized constructor.
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thank you
 
birun takhellambam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add the following constructor to Inherit.java, :

//need a default constructor
public Inherit()
{}
//end of constructor
.....
try it.
 
Rahul Kurup
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create object for InheritB(child class) a constructor for its parent class will be called implicitly. By default, the compiler looks for the zero argument constructor in the parent class. Now you have explicitly provided a constructor for Inherit class. If you hadn't, then compiler would have provided a zero argument constructor for you.
So there are 2 solutions for the program to work as described by the previous replies. One is, provide a zero argument constructor in Inherit class in addition to the already existing constructor. Second one, is by telling compiler not to look for the default zero argument constructor in parent class and call the one which you have created(using super). Since i and j will be initialized using super call, you don't have to explicitly initialize them in InheritB constructor.


Rahul
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need parameters for your constructor in the superclass, then don't add a no-parameter constructor. Put the appropriate arguments in the super() call in the subclass constructor. If you search, you can find some old threads which are helpful: here are a few which might or might not help 1, 2, 3.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic