Accessing parent class attribute using a subclass method

Report a typo

Consider a scenario where we have a 'ParentClass' with a String attribute 'familyName'. Another class 'ChildClass' is a subclass of 'ParentClass', and a method 'getFamilyName' in 'ChildClass' returns the 'familyName'. There's also a 'Main' class where we create an instance of 'ChildClass' and print the family name. Fill in the blanks in the given Java code so it compiles without errors and displays the family name when run.

Fill in the gaps with the relevant elements
 class ParentClass {
     String familyName = "Doe";
}

public class ChildClass  ParentClass{
    public String getFamilyName() {
        return familyName;
    }
}

public class Main {
    public static void main(String[] args) {
        ChildClass child = new ChildClass();
        System.out.println(child.getFamilyName());
    }
}
protectedextendspublic
___

Create a free account to access the full topic