Home > database >  In JAVA, is it possible to print something in super class method, and override and add more informat
In JAVA, is it possible to print something in super class method, and override and add more informat

Time:09-29

public class superA {
    public void display() {
        System.out.println("In super class superA");
    }
}

public class subB extends superA {
    @Override
    public void display() {
        System.out.println("In sub class subB");
    }
}

public class subC extends superA {
    @Override
    public void display() {
        System.out.println("In sub class subC");
    }
}

If an object of sub class subB calls display method, it should give output like: (new line)In super class superA (new line)In sub class subB

CodePudding user response:

The method names of all the classes parent and child is same, when you want to override superclass/parent class method with same name in child class you have to use super keyword.

CodePudding user response:

public class superA {
    public void display() {
        System.out.println("\nIn super class superA");
    }
}

public class subB extends superA {
    @Override
    public void display() {
        super.display();
        System.out.println("\nIn sub class subB");
    }
}

public class subC extends superA {
    @Override
    public void display() {
        super.display();
        System.out.println("\nIn sub class subC");
    }
}
  • Related