#348 Calling Methods Through a Parent-Type Reference

Companies:
Digitalleague
Themes:
JavaOOPCode Review

What will be printed?

Java
public class Main {

    public static void main(String[] args) {
        MedicalStaff med = new Doctor();
        med.info();
        med.prescriptionMedicine();
        med.createMedicine();
        med.staticMethod();
    }


}

class MedicalStaff {

    public static void staticMethod() {
        System.out.println("static medical");
    }

    public void prescriptionMedicine() {
        System.out.println("prescriptionMedicine");
    }

    public void info() {
        System.out.println("MedicalStaff");
    }

}

class Doctor extends MedicalStaff {

    static {

    }

    static String str = "";

    public static void staticMethod() {
        System.out.println("static doctor");
    }

    {
        int abc = 1;
    }

    public void createMedicine() {
        System.out.println("createMedicine");
    }

    @Override
    public void info() {
        System.out.println("Doctor");
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in