How to implement such kind of codes
class A{
protected int getVal() { return 2; }
protected int getValue() { return getVal(); }
}
class B extends A {
protected int getVal() { return 4; }
public int getValue() {
return getVal() + this.getVal() + super.getVal() + super.getValue();
}
}
class C extends B {
public int getVal() { return 8; }
public int getValue() {
return super.getVal() + getVal() + super.getValue() + this.getVal();
}
}
hey guys I was given the above peace of code in my Quize And I was asked, when we call the method getValue() from class C then what value will we get. well my answer was 4 + 8 + [8 + 4 + 2 + 2] + 8 = 36 which turns out to be false :eek: ( The right answer is 46 THEY TOLD ME) . I tried to implement the above mentioned code on my computer but it doesn't work. :confused:
What I did at home is create 3 Classes A, B, C and then put the main method in Class C (which is used to run the code) but it doesn't seem to work. This is what I did in class C
public class C extends B{
public int getVal() { return 8; }
public int getValue() {
return super.getVal() + getVal() + super.getValue() + this.getVal();
}
public static void main(String[] args){
System.out.println("the real value is "+ getValue());
}
}
Can anyone explain to how to implement such types of codes for testing?? Would really your help!!!