• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Construcors with Inheritance

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In above code class A has a Explicitly declared Constructor and Class B is a Sub Class of A. When i compile this code it gives following Error at compilation.

cannot find symbol
symbol : constructor A()
location: class A
class B extends A{
^
1 error


Please anyone can explain this.



It compiles correctly after adding the default constructor to the Class A. Is it a must to define default constructor Explicitly when we are doing constructor overloading?

 
Saloon Keeper
Posts: 15510
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a new instance of a class, that class' constructor must *always* call a superclass constructor. When you don't do this, the compiler will insert a call to the default constructor for you. You can see how this leads to problems when the superclass has no default constructor.

To remedy this, you make an explicit call to one of the superclass constructors in the first statement of your subclass' constructors.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashoka Jayasekara wrote:
It compiles correctly after adding the default constructor to the Class A. Is it a must to define default constructor Explicitly when we are doing constructor overloading?



The compiler will create a default no-arg constructor, if you do not create any constructor. If you create a constructor, then you are responsible for all the constructors -- no default constructor will be created.

Henry
 
Ashoka Jayasekara
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic