Using parent constructor in a child class in Java

前端 未结 3 1889
清歌不尽
清歌不尽 2020-12-03 20:52

I have a class \"ChildClass\" that extends the class \"ParentClass\". Rather than completely replace the constructor for the parent class, I want to call the parent class\'s

相关标签:
3条回答
  • 2020-12-03 21:12

    You can reference the parent's constructor with "super", from within a child's constructor.

    public class Child extends Parent {
        public Child(int someArg) {
            super(someArg);
            // other stuff
        }
        // ....
    }
    
    0 讨论(0)
  • 2020-12-03 21:17

    To invoke a specific parent-class constructor, put super(param1, param2, ...) as the first statement in the child-class constructor body.

    0 讨论(0)
  • 2020-12-03 21:28

    You should use the super keyword.

    public ChildClass(...) {
        super(...);
    }
    
    0 讨论(0)
提交回复
热议问题