• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting error with this code  RSS feed

 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am beginner to Java.
I was trying to see working of constructor with inheritence with the following code but getting an error. I know the code is very simple but I could not find out the reason of the error. Please help.


Error :
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where have you written super(...);?
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

1. A class doesn't have default constructor (with no parameters) as well as user's defined constructor with no parameters.
2. Since B class extends class A, it implicitly adds a call "super()" to a constructor with no parameters.
Think about, like your B class would look like:

So, it is failing to compile, because you don't have no parameter constructor in your A class.
In this particular case your constructor of B class should look like:
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you would miss out the assignment to i because that is done in the superclass constructor?
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)That means every class should have default constructor (with no parameters) or user's defined constructor with no parameters ?
2)I know that we can use super class constructor in sub class with 'super' keyword, but initializing super class variable directly in the sub class constructor without using 'super' should be fine, isn't it ?
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course Campbell Ritchie is right.
I overlooked, thanks for pointing that out.

Dev Choudhary, take in account CR's comment about my post, he spotted where I did mistake.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:1)That means every class should have default constructor (with no parameters) or user's defined constructor with no parameters ?
No. It means, if there is default constructor (or not), extended class implicitly (by default) adds a call to a base class constructor with no parameters "super()". If such a constructor does not exist in base class - it fails to compile.
If there is no argument constructor or default one, you need to add call with "super" and parameters list explicitly.

[sorry for edits, found difficult to describe nicely in english]
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:No. It means, if there is default constructor (or not), extended class implicitly (by default) adds a call to a base class constructor with no parameters "super()". If such a constructor does not exist in base class - it fails to compile.
If there is no argument constructor or default one, you need to add call with "super" and parameters list explicitly.


But I would like to know why there is such compulsion for default constructors in super class ?
What if we does not require default (no-arguement) constructors at all and require only paramerized constructors ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need a no-arg-constructor in the super-class. You always need to call one of the super-class' constructors from a sub-class constructor. When you don't call super(...) from a sub-class, the compiler inserts an implicit call to super(). If the super-class doesn't have a no-argument-constructor, you need to explicitly say which one you want to invoke.

Instead of assigning i1 to i in B's constructor, pass it to A's constructor using the super keyword.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:What if we does not require default (no-arguement) constructors at all and require only paramerized constructors ?
As I said, sorry if I wasn't fully clear, if you don't have and don't want default or no arguments constructor in your super class, you define constructor you want with parameters list, AND in this case, you need to add explicit call to that constructor in your extended class. Because compiler implicitly can add only call to a no parameter constructor (for your convenience).
How the example would look if you don't want default or users defined no argument constructor in your super class:
Once again, if you wouldn't add explicit "super" call with your specified arguments list to a super class constructor, compiler for your convenience would add call to default, by making an assumption you have defined one.
Note one thing, "super" call has to be a first statement in your constructor.
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I am taking too long to understand it, but I just want to make it very clear.
Ok, that means even if do not call super class constructor explicitly from the sub class (by using 'super' keyword), then also an implicit call is made to super class default constructor from sub class ?

One last doubt - Many a times such concepts (as that of problem I mentioned above), are not mentioned straight-forward in the books (as far as the books I've referred, don't know about others). So how to get acquainted with such hidden or detailed concepts ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:Ok, that means even if do not call super class constructor explicitly from the sub class (by using 'super' keyword), then also an implicit call is made to super class default constructor from sub class?

Yes.

One last doubt - Many a times such concepts (as that of problem I mentioned above), are not mentioned straight-forward in the books (as far as the books I've referred, don't know about others). So how to get acquainted with such hidden or detailed concepts ?

I think this is mentioned in the Oracle tutorials. Other books that have given me great insight are Joshua Bloch's Effective Java and Java Puzzlers.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:Ok, that means even if do not call super class constructor explicitly from the sub class (by using 'super' keyword), then also an implicit call is made to super class default constructor from sub class ?
Yes. That word "also" just confusing and shouldn't be there, apart from that - correct.

If you're not calling "super" explicitly, compiler adds implicitly "super()" call to no arguments only constructor - in this case you need to make sure you have default or no arguments constructor in super class, so your program could compile. If you don't have no argument constructor in super class, you have to call "super" yourself with appropriate list of parameters (which are defined in your super class constructor). Call "super(list of parameters)" has to be the first statement in constructor.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:So how to get acquainted with such hidden or detailed concepts ?
If you're interested to grasp technical part of Java language, you could read one of OCAJP 7/8 certifications books. These books usually concentrates on providing you with technical details of Java programming language, rather than teaching you how to program.
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. But then do following statements contradict each other ?

1)
Stephan van Hulst wrote:
Dev Choudhary wrote:Ok, that means even if do not call super class constructor explicitly from the sub class (by using 'super' keyword), then also an implicit call is made to super class default constructor from sub class?
Yes.


2) But I've read that constructors are not inherited to sub classes
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

Constructors are not inherited. Inheritance means that methods and fields become part of the sub-type. Constructors never become part of the sub-type. That doesn't mean you can't call the super-type's constructor from the sub-type.
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Thanks a lot guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!