How can we call Super Class Constructor in Sub Class ?

Discussion in 'Object Oriented Concepts' started by vbraojava, Sep 11, 2008.

  1. vbraojava

    vbraojava New Member

    We can call SuperClass Constructor From SubClass Constructor explicitly using

    super(); or super(parameter list);

    - With super(), the SuperClass Default Constructor is called.
    - With super(parameter list), the SuperClass Argumented Constructor
    with a matching parameter list is called.

    * Invocation of a SuperClass Constructor must be the first line in the SubClass Constructor.

    - If a SubClass Constructor does not explicitly invoke a SuperClass Constructor, the Java Compiler automatically call to the Default Constructor of the SuperClass. And if the SuperClass does not have a Default Constructor, we will get a compile-time error.
     
    lipun kumar patel likes this.
  2. jayaps1

    jayaps1 New Member

    Hi vbraojava,

    I have confused in the last statement. you wrote

    - If a SubClass Constructor does not explicitly invoke a SuperClass Constructor, the Java Compiler automatically call to the Default Constructor of the SuperClass. And if the SuperClass does not have a Default Constructor, we will get a compile-time error.

    If class does not have a constructor,compiler will supply a default constructor right???? Then why we get a compiler time error??

    Please explain me once more?
     
  3. khadangap

    khadangap New Member

    Hi jayps,
    According to bravo the first statment of a constructer is Super();if u don't call forcefully(explicitly).
    Ex:
    Public class ConstructerClass{
    ConstructerClass(){
    super();
    }
    }
    In the above class when you don't write The super() in the constructerClass() then at the time of compilation a default super() call will be add by the compiler.Which is nothing but the constructer of Object
    class .
    Ex2:
    public class A{
    A(){
    }
    }
    public class B extends A{
    B(int x){
    //here the first call is to super class default constructer i.e super()
    }
    }
    when you create "new B(5)" then it will compile and run fine because there is a default constructer (written by us )is present .
    Ex3:
    public class A{
    }
    public class B extends A{
    B(int x){
    //here the first call is to super class default constructer i.e super()
    }
    }
    by creating "new B(5)"
    this also compiles fine because implicitly there is a default constructer
    Ex4:
    public class A{
    A(String s){
    }
    }
    public class B extends A{
    B(int x){
    //here the first call is to super class default constructer i.e super()
    }
    }
    in this case by creating "new B(5)" we will get compilation error because there is no implicit or explicit default constructor called by B's constructer .
    So to compile and run successfully u have to write a default constructer in class A. As
    EX.5
    public class A{
    A(){
    }
    A(String s){
    }
    }
    public class B extends A{
    B(int x){
    //here the first call is to super class default constructer i.e super()
    }
    }
    Important note for your Q
    A compiler write a default constructer for you if you dont write any constructer ; if u write an arguemented constructer then compile will not write a constructer for you . so be care full.
     
    Last edited: Dec 12, 2009
  4. jayaps1

    jayaps1 New Member

    Thanks a lot khadangap.
    Now i got the point.
     
  5. naveenm_006

    naveenm_006 New Member

    Hi,

    What will happen if the subclass doesn't have construtor will it call super class default constructor?
    Will the complier provides default construtor for both these calsses?
    eg:

    class super{

    }
    class sub extends super{


    p.v.s.m(String args[]){
    sub s=new sub();
    }

    Regards,
    Naveen.
     
  6. khadangap

    khadangap New Member

    Hi Naven,
    class super{
    //super(){super();}
    }
    class sub extends super{
    //sub(){super();}
    p.v.s.m(String args[]){
    sub s=new sub();
    }

    Yes you are right ,
    if you don't write a constructer explicitly then compiler will add a constructer for u as in the highlighted statements in above .
    ex:
    class A{
    B]//A(){super();}[/B]
    }

    in the above example if you dont write the constructor(just delete the comment line) ;you want to create a super object like
    Super super = new Super();
    then at first the constructer of super class will be called ,in the above class
    A() when control enters in to this constructor then its 1st statement is
    super()(which is default) and is the constructer of
    Object class . when control enters to the object class's constructer it initialises the object class and return back to the class A. and intialises it and creats an object .
    I think it will help you
     
  7. suneetha211

    suneetha211 New Member

    super() is used to invoke a super class constructor.

    this must be the firststatement in the class
     
  8. chsrinu

    chsrinu New Member

    we can call super class constructor by using the
    super(parameternames);

    The above statement is first statement is subclass constructor
     
  9. anneroberts

    anneroberts Member

    The suAN occurrence from claiming guardian class may be made implicitly which may be alluded Toward super reference variable. Will allude prompt guardian population occurrence variable. Super camwood be used to conjure prompt guardian class system. Super() could be used to conjure prompt guardian class constructor.per keyword in java is a reference variable which is used to refer immediate parent class object.
     

Share This Page