• 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

constructor

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B page no 319:

"if your superclass doesnot have a no-arg constructor you must type a constructor in your class(subclass) because you need a place to put in the call to super with apropriate arguments."

can someone explain this with example?

i dont understand the above statement because evenif superclass has no arg constructor still you need to have a constructor in your subclass with super()(call to superclass constructor).you just dont have to provide the arguments there.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swapnil,

Its like this, if u dont have no args constructor in the supercalss i.e superclass is having all constructors with arguments, then in the constructor of the baseclass we have to give an explicit call to the superclass constructor with apropriate arguments otherwise there will be compile time error that 'there is no such constructor Superclass()']

Look at the example below

class Superclass()
{
Superclass(int a){}
Superclass(float b){}
}

public class Baseclass() extends Superclass
{
Baseclass()
{
/*if we dont inclde line 2 ,then compiler will insert a default call to Superclass construcor here i.e. super() i.e super with no args which is not defined in superclass ,hence it will give compile time error*/
super(10); //(2)
}

I hope this will help u.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If We Dont Have Costrutor To subclass, java implicitly create One no arg construtor. (i.e. default Costructor) This Constructor Has A Call To The default Constuctor Of Super Class. As Java provides implicit constuctor Only for those classes who dont have Any Constructor.

Hence to Avoid All Of default Constuctor Of Super Class We Must Have An Explict Constuctor For subclass To call Predefined parametric constuctor of super class.
 
swapnil paranjape
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you swati and rushikesh..thathanx for the help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic