The super Keyword in Java


Use super to refer to superclass (parent) objects, methods, and constructors.

Source Code

class Superclass {
    void printMethod() {
        System.out.println("Printed in Superclass.");
    }
}
class Subclass extends Superclass {
    void printMethod() {
        super.printMethod(); // Calls the method in Superclass
        System.out.println("Printed in Subclass");
    }
    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.printMethod();
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments