Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Call Overriden Method from Constructor

807600Jun 26 2007 — edited Jun 27 2007
class Super {
    Super(){
        System.out.println("In Super constructor");
        test();
    }
    
    void test() {
        System.out.println("In Super.test()");
    }
}

class Sub extends Super {
    Sub() {
        System.out.println("In Sub constructor");
    }
    
    void test() {       // overrides test() in Super
        System.out.println("In Sub.test()");
    }
}

Output - In Super Constructor
               In Sub.test()
               In Sub Constructor
Suppose i create a subclass object, so at first superclass's constructor will be called, but then how can it call subclass's test, since the object for the derived class and method's implementation has not been allocated memory?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 25 2007
Added on Jun 26 2007
11 comments
212 views