Results 1 to 6 of 6
- 11-08-2010, 11:20 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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!!!
- 11-09-2010, 01:11 AM #2
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
first, you must instantiate the class
C whatEverVariables = new C();
then, print out whatEverVariables.getValue()
- 11-09-2010, 03:21 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
WOW!!! it was that easy... Can't believe why didn't I think of that... Thanks a lot man..
- 11-09-2010, 04:13 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
If you didn't think of that, perhaps it would be a good time to read up on the difference between static and non-static methods and objects in general. These are the basics you need to understand before you can do more complex stuff. Much luck!
Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-09-2010, 04:50 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Do you understand why 46 is the correct answer now?
- 11-10-2010, 05:47 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Kind of stuck
By Nicky Swans in forum New To JavaReplies: 8Last Post: 10-22-2010, 02:46 PM -
kind of Component being return? and Conversion
By mine0926 in forum NetBeansReplies: 11Last Post: 05-17-2010, 10:11 AM -
';' expected, not of the common kind..
By Addez in forum New To JavaReplies: 6Last Post: 09-02-2009, 04:37 AM -
how client know what kind of server
By lemur in forum NetworkingReplies: 3Last Post: 05-31-2008, 07:11 AM -
Total Newbie, Be Kind :)
By dazza-s in forum New To JavaReplies: 11Last Post: 04-26-2008, 10:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks