Results 1 to 6 of 6
  1. #1
    khaniazi is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Question 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!!!

  2. #2
    dandybreath is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    first, you must instantiate the class
    C whatEverVariables = new C();
    then, print out whatEverVariables.getValue()

  3. #3
    khaniazi is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    WOW!!! it was that easy... Can't believe why didn't I think of that... Thanks a lot man..

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,932
    Rep Power
    19

    Default

    Do you understand why 46 is the correct answer now?

  6. #6
    dandybreath is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Kind of stuck
    By Nicky Swans in forum New To Java
    Replies: 8
    Last Post: 10-22-2010, 02:46 PM
  2. kind of Component being return? and Conversion
    By mine0926 in forum NetBeans
    Replies: 11
    Last Post: 05-17-2010, 10:11 AM
  3. ';' expected, not of the common kind..
    By Addez in forum New To Java
    Replies: 6
    Last Post: 09-02-2009, 04:37 AM
  4. how client know what kind of server
    By lemur in forum Networking
    Replies: 3
    Last Post: 05-31-2008, 07:11 AM
  5. Total Newbie, Be Kind :)
    By dazza-s in forum New To Java
    Replies: 11
    Last Post: 04-26-2008, 10:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •