"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.
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) }
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.
Best Regards,
Rushikesh Wagh
SCJP 1.4 (2005)
SCJP 1.6 (2010)