Inheritence - Study Mode

[#71] What will be the output? interface A{
public void method1()
}
class One implements A{
public void method1(){
System.out.println("Class One method1")
}
}
class Two extends One{
public void method1(){
System.out.println("Class Two method1")
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two()
a.method1()
}
}
Correct Answer

(C) Class Two method1

[#72] What is the result of compiling and running this program? class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food")
}
}
class Cattle extends Mammal{
void eat(Cattle c){
System.out.println("Cattle eats hay")
}
}
class Horse extends Cattle{
void eat(Horse h){
System.out.println("Horse eats hay")
}
}
public class Test{
public static void main(String[] args){
Mammal h = new Horse()
Cattle c = new Horse()
c.eat(h)
}
}
Correct Answer

(A) prints "Mammal eats food"

[#73] Determine output: class A{
public void method1(){
System.out.print("Class A method1")
}
}
class B extends A{
public void method2(){
System.out.print("Class B method2")
}
}
class C extends B{
public void method2(){
System.out.print("Class C method2")
}
public void method3(){
System.out.print("Class C method3")
}
}
public class Test{
public static void main(String args[]){
A a = new A()
C c = new C()
c.method2()
a = c
a.method3()
}
}
Correct Answer

(C) Compilation Error

Explanation

Solution: It is important to understand that it is the type of reference variable - not the type of the object that it refers to - that which determines what members can be accessed. That is, when a reference to a subclass object is assigned to a super class reference variable, we will have access only to those parts of the object defined by the superclass. In the above program method method3() is defined in the class C which is a subclass of B and so A . Even the reference variable a refers to c , a can't access method3() as this method is unknown to class A .

[#74] What will be printed after executing following program code? class Base{
int value = 0
Base(){
addValue()
}
void addValue(){
value += 10
}
int getValue(){
return value
}
}
class Derived extends Base{
Derived(){
addValue()
}
void addValue(){
value += 20
}
}
public class Test{
public static void main(String[] args){
Base b = new Derived()
System.out.println(b.getValue())
}
}
Correct Answer

(C) 40

Explanation

Solution: When object of new derived is called, the flow goes to Derived() first, by default super()
is present in Derived() as the first statement, so the flow now goes to Base. Here value is initialised to 0 and then addValue() is called.
The addValue has been overridden in Derived() hence The Base's addValue() will perform value+20(0+20) .After this control flows back to Derived()'s addValue() where again value+20 is done (20+20). Hence Answer is 40

[#75] What will be the output? class Parent{
public void method(){
System.out.println("Hi i am parent")
}
}
public class Child extends Parent{
protected void method(){
System.out.println("Hi i am Child")
}
public static void main(String args[]){
Child child = new Child()
child.method()
}
}
Correct Answer

(C) Compile time error

Explanation

Solution: We cannot reduce the visibility of the inherited method from super class. If the overridden or hidden method is public, then the overriding or hiding method must be public
otherwise, a compile-time error occurs. If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public
otherwise, a compile-time error occurs. If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private
otherwise, a compile-time error occurs.