OneCompiler

Inheritance and Method Overriding in Java

245

public class Main {
public static void main(String[] args) {
Child child = new Child(10, 20);
child.display();
}
}
class Parent {
int value;

Parent(int value) {
    this.value = value;
}

void display() {
    System.out.println("Value in Parent: " + value);
}

}

class Child extends Parent {
int value;

Child(int parentValue, int childValue) {
    super(parentValue); // Calling the constructor of the parent class
    this.value = childValue;
}

void display() {
    super.display(); // Calling the display method of the parent class
    System.out.println("Value in Child: " + value);
}

}