Is it possible to override super class constructor?

Is it possible to override super class constructor form subclass?

However, you can achieve do something that looks similar to overriding.
class Parent {
  Parent(int i, String s) {
    // do stuff
class Child extends Parent {
  Child(int i, String s) {
    super(i, s);
    // do Child stuff here
new Parent(1, "abc");
new Child(2, "xyz");Although that's not overriding, it sort of looks similar. Is this what you were talking about?

Similar Messages

  • How to inherit super class constructor in the sub class

    I have a class A and class B
    Class B extends Class A {
    // if i use super i can access the super classs variables and methods
    // But how to inherit super class constructor
    }

    You cannot inherit constructors. You need to define all the ones you need in the subclass. You can then call the corresponding superclass constructor. e.g
    public B() {
        super();
    public B(String name) {
        super(name);
    }

  • Don't need call to super class constructor

    Hi, I have
    class A {
    public A() { System.out.println("in A()");}
    public class B extends A
    { public B() {
    System.out.println("in B()");}
    public static void main( String args[] ) {
    B b = new B();}
    I don't want the super class constructor to be called... What to do? Please tell me solution other than usage of parameterised constructor?

    I don't want the super class constructor to be called... What to do? Can't be done. One of the constructors of the super class must be invoked. (Implicitly or explicitly)
    Kaj

  • Can defualt inherited the Super Class constructor to sub class ?

    Hi,
    is it passible to inherit the super class constructor by defualt when extends the super class.
    Thanks
    MerlinRoshina

    Constructor rules:
    1) Every class has at least one ctor.
    1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().
    1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...} if you want one.
    1.3) Constructors are not inherited.
    2) The first statement in the body of any ctor is either a call to a superclass ctor super(...) or a call to another ctor of this class this(...) 2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super() as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.
    2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

  • Invoke super class constructor of super class' parent class

    I would like to invoke a constructor of a super class that is the parent of the direct super class. For instance:
    class C extends class B, and class B extends class A.
    From class C, is it possible to invoke class A's constructor without first invoking class B constructor?
    Something like super.A() ??
    Thanks.
    Joe

    try this,
    abstract class GrandParent
          private Object pObj = null;
          protected Via via = new Via();
          protected class Via
             public Object doMethod1( Object obj )
                pObj = obj;
                return "GrandParent m1: " + pObj;
             public Object doMethod2( Object obj )
                pObj = obj;
                return "GrandParent m2: " + pObj;
             public Object get()
                return "gp get: " + pObj;
          public abstract Object doMethod1( Object obj );
          public abstract Object doMethod2( Object obj );
          public abstract Object get();
    class Parent extends GrandParent
          private Object pObj = null;
          public Object doMethod1( Object obj )
          pObj = obj;
          return "Parent m1: " + pObj;
          public Object doMethod2( Object obj )
          pObj = obj;
          return "Parent m2: " + pObj;
          public Object get()
          return "p get: " + pObj;
    public class GPMethod extends Parent
          public static void main( String[] args )
          GrandParent gp = new Parent();
          System.out.println( gp.doMethod1( "calling parent" ) );
          System.out.println( gp.doMethod2( "calling parent m2" ) );
          System.out.println( gp.get() );
          System.out.println( "" );
          System.out.println( gp.via.doMethod1( "calling via to GP" ) );
          System.out.println( gp.via.doMethod2( "calling via to GP m2" ) );
          System.out.println( gp.via.get() );
    }

  • Invoked super class constructor means create an object of it?

    Hei,
    i have create one class that extends another class. when i am creating an object of sub class it invoked the constructor of the super class. thats okay.
    but my question is :
    constructor is invoked when class is intitiated means when we create an
    object of sub class it automatically create an object of super class. so means every time it create super class object.

    Hi,
       An object has the fields of its own class plus all fields of its parent class, grandparent class, all the way up to the root class Object. It's necessary to initialize all fields, therefore all constructors must be called! The Java compiler automatically inserts the necessary constructor calls in the process of constructor chaining, or you can do it explicitly.
       The Java compiler inserts a call to the parent constructor (super) if you don't have a constructor call as the first statement of you constructor.
    Normally, you won't need to call the constructor for your parent class because it's automatically generated, but there are two cases where this is necessary.
       1. You want to call a parent constructor which has parameters (the automatically generated super constructor call has no parameters).
       2. There is no parameterless parent constructor because only constructors with parameters are defined in the parent class.
       I hope your query is resolved. If not please let me know.
    Thanks & Regards,
    Charan.

  • Overriding super class methods @ OIM 9x

    Hi All,
    I'm working on a requirement where I need to override superclass method in sub class.The problem is, method defined in sub class is not getting called by OIM.
    My question more looks like Pure Java related,but as it is OIM which uses Struts, calling of ActionForward methods defined in
    protected Map getKeyMethodMap()
    +{+
    +}+
    Super class is OUT OF BOX class and sub class is I m developing.Both superclass & subclass having the protected Map getKeyMethodMap() method with same declaration.
    Can any one give me a clue on this.
    Regards,
    Krish

    HI All,
    Issue resolved after deleting stage & tmp folders in weblogic instance directory and restarted weblogic instance.
    Regards,
    Krish

  • Using super class constructor in subclass constructor

    Hi all. How can I use Person() in my Client()?
    something like this:
    public class Client extends Person{
          private float balance;
          Client(String firstName,String midleName,String lastName,Contacts contacts,Adress adress,float balance){
               this= Person(firstName,midleName,lastName,contacts,adress);
               this.balance=balance;
          }

    public class Client extends Person{
      private float balance; // float?!? Are you sure.... ?
      Client(String firstName,String midleName,String lastName,Contacts contacts,Adress adress,float balance){
        super(firstName,midleName,lastName,contacts,adress);
        this.balance=balance;
    }

  • Reference to enclosing instance in inner class constructor

    Is there any Java compiler which assigns reference to enclosing instance in constructor of inner clase before invoking super class constructor?
    class Outer {
    class Inner extends Global {
    public Inner(int x) {
    // I want (Outer.this != null) here
    super();
    class Global {
    public Global(int x) {

    class Outer {
    class Inner extends Global {
    public Inner(int x) {
    // I want (Outer.this != null) hereOuter.this is never null at this point. A non-static
    inner class always has an implicit reference to an
    instance of the enclosing class.Try this:
    class Outer {
    int m;
    class Inner extends Global {
    public Inner(int x) {
    super(x);
    protected void init(int x) {
    xxx = Outer.this.m + x; // Null pointer exception!!!
    class Global {
    int xxx;
    public Global(int x) {
    init(x);
    protected void init(int x) {
    xxx = x;

  • Even i call this() paramerterised constuctor in my class, Is super class su

    Even i call this() paramerterised constuctor in my class, Is super class super() parameter less constuctor invokes?
    I have excuted the following program. And result will be as follows:
    Grandparent() constructor
    Parent 25 constructor
    Parent() constructor
    Child() constructor
    The program is below:
    class ConstructorChain {
    public static void main(String[] args) {
    Child c = new Child();
    class Child extends Parent {
    Child() {
    System.out.println("Child() constructor");
    class Parent extends Grandparent {
    Parent() {
    this(25);
    System.out.println("Parent() constructor");
    Parent(int x) {
    System.out.println("Parent(" + x + ") constructor");
    class Grandparent {
    Grandparent() {
    System.out.println("Grandparent() constructor");
    for this my excepected answer is
    Parent 25 constructor
    Parent() constructor
    Child() constructor
    because in parent class i defined this() constructor so, as my undersatnd is it never call Grand Parent Constructor as it is default parameterless constructor.
    please Advise me
    Aruna

    You can't initialize a class without it's super classes constructor being run. So even if you call another constructor (of the same class), the parent classes constructor will still be called.

  • How to override a method in an inner class of the super class

    I have a rather horribly written class, which I need to adapt. I could simply do this if I could override a method in one of it's inner classes. My plan then was to extend the original class, and override the method in question. But here I am struggling.
    The code below is representative of my situation.
    public class ClassA
       ValueChecks vc = null;
       /** Creates a new instance of Main */
       public ClassA()
          System.out.println("ClassA Constructor");
          vc = new ValueChecks();
          vc.checkMaximum();
         // I want this to call the overridden method, but it does not, it seems to call
         // the method in this class. probably because vc belongs to this class
          this.vc.checkMinimum();
          this.myMethod();
       protected void myMethod()
          System.out.println("myMethod(SUPER)");
       protected class ValueChecks
          protected boolean checkMinimum()
             System.out.println("ValueChecks.checkMinimum (SUPER)");
             return true;
          protected boolean checkMaximum()
             return false;
    }I have extended ClassA, call it ClassASub, and it is this Class which I instantiate. The constructor in ClassASub obviously calls the constructor in ClassA. I want to override the checkMinimum() method in ValueChecks, but the above code always calls the method in ClassA. The ClassASub code looks like this
    public class ClassASub extends ClassA
       public ClassAInner cias;
       /** Creates a new instance of Main */
       public ClassASub()
          System.out.println("ClassASub Constructor");
       protected void myMethod()
          System.out.println("myMethod(SUB)");
       protected class ValueChecks extends ClassA.ValueChecks
          protected boolean checkMinimum()
             System.out.println("ValueChecks.checkMinimum (SUB)");
             return true;
    }The method myMethod seems to be suitably overridden, but I cannot override the checkMinimum() method.
    I think this is a stupid problem to do with how the ValueChecks class is instantiated. I think I need to create an instance of ValueChecks in ClassASub, and pass a reference to it into ClassA. But this will upset the way ClassA works. Could somebody enlighten me please.

    vc = new ValueChecks();vc is a ValueChecks object. No matter whether you subclass ValueChecks or not, vc is always of this type, per this line of code.
    // I want this to call the overridden method, but it does not, it seems to > call
    // the method in this class. probably because vc belongs to this class
    this.vc.checkMinimum();No, it's because again vc references a ValueChecks object, because it was created as such.
    And when I say ValueChecks, I mean the original class, not the one you tried to create by the same name, attempting to override the original.

  • Is it possible to OVERLOAD a super-class method in a sub-class?

    Hi all,
    I have a query that
    Is it possible to OVERLOAD a super-class method in a sub-class?
    If it is possible, please give me an example.
    Thanks,
    Hari

    Hi,
    Is the method int Display(int a){} overloading
    the super-class's void Display() method? If
    possible, please clarify this and how it would be
    method overloading?
    hanks,
    Hari
    Hi Hari,
    Yes, it is possible. Look at this piece of code:
    class Senior
         void Display()
              System.out.println("Super class method");
    class Junior extends Senior
         int Display(int a)
              System.out.println("Subclass method: "+a);
              return(a+10);
         }> }
    class example
         public static void main(String args[])
              Junior j = new Junior();
              j.Display();
    System.out.println("Subclass method
    od "+j.Display(5));
    Is this what you were asking? Hope this helped.Hi,
    I guess you guys are confused here...
    Overloading is achieved by methods in the same class...
    Overriding is across a superclass subclass methds.

  • Error while calling a super class public method in the subclass constructor

    Hi ,
    I have code like this:
    CLASS gacl_applog DEFINITION ABSTRACT.
      PUBLIC SECTION.
        METHODS:
                create_new_a
                   IMPORTING  pf_obj       TYPE balobj_d
                              pf_subobj    TYPE balsubobj
                              pf_extnumber TYPE string
                   EXPORTING  pfx_log_hndl TYPE balloghndl
                   EXCEPTIONS error
    ENDCLASS.
    CLASS gacl_applog IMPLEMENTATION.
      METHOD create_new_a.
        DATA: ls_log TYPE bal_s_log.
      Header aufsetzen
        MOVE pf_extnumber TO ls_log-extnumber.
        ls_log-object     = pf_obj.
        ls_log-subobject  = pf_subobj.
        ls_log-aluser     = sy-uname.
        ls_log-alprog     = sy-repid.
        ls_log-aldate     = sy-datum.
        ls_log-altime     = sy-uzeit.
        ls_log-aldate_del = ls_log-aldate + 1.
        CALL FUNCTION 'BAL_LOG_CREATE'
             EXPORTING
                  i_s_log      = ls_log
             IMPORTING
                  e_log_handle = pfx_log_hndl
             EXCEPTIONS
                  OTHERS       = 1.
        IF ( sy-subrc NE 0 ).
          MESSAGE ID      sy-msgid TYPE sy-msgty NUMBER sy-msgno
                  WITH    sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
                  RAISING error.
        ENDIF.
      ENDMETHOD.
    CLASS gcl_applog_temp DEFINITION INHERITING FROM gacl_applog.
      PUBLIC SECTION.
        DATA: log_hndl   TYPE balloghndl READ-ONLY
            , t_log_hndl TYPE bal_t_logh READ-ONLY
        METHODS: constructor
                   IMPORTING  pf_obj       TYPE balobj_d
                              pf_subobj    TYPE balsubobj
                              pf_extnumber TYPE string
                   EXCEPTIONS error
               , msg_add      REDEFINITION
               , display      REDEFINITION
    ENDCLASS.
    CLASS gcl_applog_temp IMPLEMENTATION.
      METHOD constructor.
        CALL METHOD create_new_a
               EXPORTING  pf_obj       = pf_obj
                          pf_subobj    = pf_subobj
                          pf_extnumber = pf_extnumber
               IMPORTING  pfx_log_hndl = log_hndl.
        IF ( sy-subrc NE 0 ).
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
                  RAISING error.
        ENDIF.
      ENDMETHOD.
    A public method of Super class has been called from the constructor of the sub class. we are getting the syntax error :
    ' In the constructor method, you can only access instance attributes, instance methods, or "ME" after calling the constructor of the superclass…'
    Can you please suggest how to change the code with out affecting the functioanlity.
    Thank you ,
    Lakshmi.

    Hi,
    Call that method by instance of Subclass.   OR
    SUPER-->method.
    Read very useful document
    Constructors
    Constructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the starting state of a new object or class. There are two types of constructors - instance constructors and static constructors. Constructors are methods with a predefined name. To use them, you must declare them explicitly in the class.
    The instance constructor of a class is the predefined instance method CONSTRUCTOR. You declare it in the public section as follows:
    METHODS CONSTRUCTOR
            IMPORTING.. [VALUE(]<ii>[)] TYPE type [OPTIONAL]..
            EXCEPTIONS.. <ei>.
    and implement it in the implementation section like any other method. The system calls the instance constructor once for each instance of the class, directly after the object has been created in the CREATE OBJECT statement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS additions in the CREATE OBJECT statement.
    The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR. You declare it in the public section as follows:
    CLASS-METHODS CLASS_CONSTRUCTOR.
    and implement it in the implementation section like any other method. The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the components of its own class.
    Pls. reward if useful....

  • Calling constructor of super class

    Hello everyone! I'm a student. I hope I can find guidance. Here's the issue:
    Super class: Property
    Sub class: House
    constructor of parent class:
    protected Property(String id, char status, String address,
            String tenet, String landlord, long rent, char freq,
            long amtDue, String date, boolean repair, char tradesman)
            this.id = id;
            this.status = status;
            this.address = address;
            this.tenet = tenet;
            this.landlord = landlord;
            this.rent = rent;
            this.freq = freq;
            this.amtDue = amtDue;
            this.repair = repair;
            this.tradesman = tradesman;
            SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yy");
            this.dateDue = formatter.parse(date, new ParsePosition(0));
        }the constuctor of the sub class:
    public House(String id, char status, String address,
            String tenet, String landlord, long rent, char freq,
            long amtDue, String dateDue, boolean repair, char tradesman,
            int broom, int proom, int throom, boolean garden,
            boolean garage, boolean heating)
        super(id, status, address, tenet, landlord, rent, freq,
            amtDue, dateDue, repair, tradesman);
        this.broom = broom;
        this.proom = proom;
        this.throom = throom;
        this.garden = garden;
        this.garage = garage;
        this.heating = heating;
        }I'm sure you notice I'm calling the constructor of the Property in House. That;s where the error is when i try to compile either class. This is the error it shows within the House class.
    Object() in java.lang.Object cannot be applied to (java.lang.String,char,java.lang.String,java.lang.String,java.lang.String,long,char,long,java.lang.String,boolean,char)
    Maybe its elementary? Thanks in advance..

    You haven't pasted all the code so I can only guess that the class House does not extend Property class.
    public class House extends Property
      // put the constructor here
    }If the House class does not extend Property (or any other class) it just extends (by default) the Object class, which does not have the constructor of Property class.
    Hope it helps
    Nick

  • Constructor of derived-class has to call constructor  of super-class?

    In java, constructor of derived-class has to call constructor of super-class? there is no way to omit this step?

    Correct. If you do not explicitly call the constructor, a call to the no-arg c'tor, super(), is automatically inserted.
    It would be a mess to have it any other way. You'd be creating objects that are not completely initialized. They'd be in an invalid state.
    Constructor rules:
    1) Every class has at least one ctor.
    1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().
    1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...} if you want one.
    1.3) Constructors are not inherited.
    2) The first statement in the body of any ctor is either a call to a superclass ctor super(...) or a call to another ctor of this class this(...) 2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super() as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.
    2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

Maybe you are looking for