Results 1 to 2 of 2
  1. #1
    amol84 is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Question the explanation of output of simple java program

    class Base {
    int i=99;
    public void amethod(){
    System.out.println("Base.amethod()");
    }
    }

    public class RType extends Base{
    int i=-1;
    public static void main(String argv[]){
    Base b = new RType();//<= Note the type
    System.out.println(b.i);
    b.amethod();
    }

    public void amethod(){
    System.out.println("RType.amethod()");
    }
    }

    I complied the above code and got the following output :
    99
    RType.amethod()

    Can someone explain why I got 99 instead of -1 .
    Thanks
    Amol

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    [b]Base b[/b] = new RType();//<= Note the type
    System.out.println(b.i);
    Base b, not RType b.
    And b.i = 99.
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Replies: 4
    Last Post: 10-02-2008, 10:05 PM
  2. Program can run but output all null
    By matt_well in forum New To Java
    Replies: 15
    Last Post: 07-24-2008, 08:48 AM
  3. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 03:27 AM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM

Posting Permissions

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