Java Program using super and this keyword


In this program, You will learn how to implement super and this keyword in Java.


super.display( int ) { this.x = x }


Example: How to implement super and this keyword in Java

Run it Now
class First {
    int x;
    void display(int x) {
        this.x = x;
        System.out.println("super method is called here");
    }

    void print() {
        System.out.println("The x value in parent :" + x);
    }
}

class Main extends First {

    int x = 20;

    void display() {
        super.display(x);
        System.out.println("x value in child :" + x);
    }

    public static void main(String args[]) {
        Main t = new Main();
        t.display();
        t.print();
    }
}

Output:

super method is called here
x value in child :20
The x value in parent :20