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.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClasses and objectsAccess control
Protected modifier
Accessing parent class attribute using a subclass method
Report a typo
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());
}
} ___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.