Any way to _not_ call superclass constructor in Java?

后端 未结 13 1397
误落风尘
误落风尘 2020-12-15 04:16

If I have a class:

class A {
   public A() { }
}

and another

class B extends A {
   public B() { }
}

is t

13条回答
  •  一个人的身影
    2020-12-15 04:19

    The possibility is that you can call the super class constructor of your choice. That is possible by calling the super class constructor explicitly as :

    super(para_1, para_2,........);
    
    
    class BaseA {
        BaseA(){
            System.out.println("This is BaseA");
        }
        BaseA(int a){
            System.out.println("This is BaseA a");
        }
    
    
    }
    
    class A extends BaseA {
    
        A(){
            super(5);
            System.out.println("This is A");
        }
    
        public static void main(String[] args) {
            A obj=new A();
    
        }
    }
    
    
    
    Output will be:
    This is BaseA a
    This is A
    

提交回复
热议问题