Interpret which of the following statements are correct with respect to inheritance relationship in java? Select one or more:a.object of subclass referenced by super class type can access super class variablesb.object of subclass referenced by super class type can invoke super class methodsc.object of subclass referenced by super class type can access newly defined sub class variablesd.object of subclass referenced by super class type can invoke newly defined sub class methodse.object of subclass referenced by super class type can invoke overridden sub class methods
Question
Interpret which of the following statements are correct with respect to inheritance relationship in java? Select one or more:a.object of subclass referenced by super class type can access super class variablesb.object of subclass referenced by super class type can invoke super class methodsc.object of subclass referenced by super class type can access newly defined sub class variablesd.object of subclass referenced by super class type can invoke newly defined sub class methodse.object of subclass referenced by super class type can invoke overridden sub class methods
Solution 1
The correct statements with respect to inheritance relationship in Java are:
a. Object of subclass referenced by super class type can access super class variables: This is correct. In Java, a subclass inherits all the members (fields, methods, and nested classes) from its superclass. Therefore, an object of the subclass can access the superclass's variables.
b. Object of subclass referenced by super class type can invoke super class methods: This is also correct. A subclass can invoke the superclass's methods.
c. Object of subclass referenced by super class type can access newly defined subclass variables: This is incorrect. If the object is referenced by the superclass type, it cannot access variables that are only defined in the subclass.
d. Object of subclass referenced by super class type can invoke newly defined subclass methods: This is also incorrect. If the object is referenced by the superclass type, it cannot invoke methods that are only defined in the subclass.
e. Object of subclass referenced by super class type can invoke overridden subclass methods: This is correct. If a subclass has overridden a method from the superclass, then the object of the subclass, even when referenced by the superclass type, will invoke the overridden method in the subclass. This is due to the runtime polymorphism in Java.
Solution 2
The correct statements with respect to inheritance relationship in Java are:
a. Object of subclass referenced by super class type can access super class variables: This is correct. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Therefore, an object of the subclass can access the superclass's variables.
b. Object of subclass referenced by super class type can invoke super class methods: This is also correct. A subclass can invoke the superclass's methods.
c. Object of subclass referenced by super class type can access newly defined subclass variables: This is incorrect. If the reference type is of superclass, it cannot access the variables that are only defined in the subclass.
d. Object of subclass referenced by super class type can invoke newly defined subclass methods: This is also incorrect. If the reference type is of superclass, it cannot invoke the methods that are only defined in the subclass.
e. Object of subclass referenced by super class type can invoke overridden subclass methods: This is correct. If a subclass has overridden a method from the superclass, then the object of the subclass, even when referenced by a superclass type, will invoke the overridden method in the subclass. This is due to the dynamic method dispatch or runtime polymorphism in Java.
Similar Questions
Which of the following is a representation of a subclass inheriting data members of its superclass?Optionsclass SubClass extends SuperClass {}class SubClass { extends SuperClass } class SubClass: SuperClass {}class SubClass inherits SuperClass {}
Which of the following statements are true? Please select all that apply. Group of answer choicesA subclass is a subset of a superclass.A subclass is usually extended to contain more functions and more detailed information than its superclass."class A extends B" means A is a subclass of B."class A extends B" means B is a subclass of A.
Which of the following statements is true about method overriding in Java?Question 1Answera.Method overriding is not allowed in Java.b.A static method can override a non-static method.c.A non-static method can override a static method.d.The overridden method in the subclass must have the same access modifier as the superclass method.
ct the correct answerIn polymorphism, a reference variable of the superclass can refer to the object of which classes?OptionsOnly the superclassAny classOnly the subclassThe superclass or any of its subclasses
Which of the following statements about inheritance and polymorphism is true in Java?Question 2Answera.Inheritance and polymorphism are independent concepts and do not impact each other.b.Inheritance is a prerequisite for polymorphism.c.Polymorphism can only be achieved through method overloading.d.Inheritance and polymorphism cannot be used together in the same program.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.