DEV Community

Aniket pagedar
Aniket pagedar

Posted on

USE OF SUPER KEYWORD

class BaseClass {
protected int x; // Integer variable in the base class

// Constructor for the base class
public BaseClass(int x) {
    this.x = x;
}

// Method to display the value of x in the base class
public void show() {
    System.out.println("BaseClass - Value of x: " + x);
}
Enter fullscreen mode Exit fullscreen mode

}

class SubClass extends BaseClass {
// Constructor for the subclass
public SubClass(int x) {
super(x); // Call the constructor of the base class
}

// Method to display the value of x in the subclass
@Override
public void show() {
    System.out.println("SubClass - Value of x: " + x);
}
Enter fullscreen mode Exit fullscreen mode

}

public class superkeywo {
public static void main(String[] args) {
// Create instances of the base class and subclass
BaseClass baseObj = new BaseClass(10);
SubClass subObj = new SubClass(20);

    // Call the show() method for the base class
    baseObj.show();

    // Call the show() method for the subclass
    subObj.show();
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)