Forum Stats

  • 3,872,715 Users
  • 2,266,464 Discussions
  • 7,911,287 Comments

Discussions

Could u tell me why in this SCJP question?

843789
843789 Member Posts: 46,655 Green Ribbon
What will happen when you attempt to compile and run the following code?
class Trebble{
    int i = 99;
}
class Base extends Trebble{
    int i =100;
}
public class Central extends Base{
    public static void main(String argv[]){
	Central c = new Central();
	c.wynyard();
    }
    public void wynyard(){
	Trebble t = new Central();
	Base b = (Base) t;
	System.out.println(b.i);
    }
	
}
1 Compile time error
2 Compilation and output of 99
3 Compilation and output of 100
4 Compilation, but runtime error because an instance of Trebble cannot be cast to type Base

I chosed 2 but the right answer is 3.
Anyone can tell me why? Thanks!

Comments

  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    The value of i in class B is 100.
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    Thank you for answering my question.
    But I still don't know it.
    Well, If it is written as follows:
    Base b = new Base();
    so I can understand that the i value is 100.
    But the code is:
    Trebble t = new Central();   //Now the i value is 99
    Base b = (Base) t;        //Here  b is created, pointing to t. so the 
                             //b.i  is t.i, and t.i is 100
    Perhaps I can run this code to see the result, but I really can't understand it. Do a member value of a class will be changed when the class is casted?
    Could you tell me more about it? Thanks a lot.
  • 3004
    3004 Member Posts: 204,171 Green Ribbon
    Which class' i to use could be determined by one of two things: The type of the reference, which is known at compile time, or the class of the object the reference points to, which is not known until runtime.

    For non-private, non-final, non-static methods, it's the runtime class that determines this. For everything else--every member that's either static, private, final, or not a method (that is, it's a variable), it's the compile-time type of the reference that determines which class' version of that membe to access.

    In this case, i is a variable, so it's "bound" at compile time by the type of the reference, which is B.
  • 3004
    3004 Member Posts: 204,171 Green Ribbon
    Trebble t = new Central(); //Now the i value is 99
    There are two i variables here--Trebble's and Base's. One of them is 99, and the other is 100.

    Base b = (Base) t; //Here b is created,
    pointing to t.
    Not quite. It doesn't point to t, because t is just a variable. It points to the same object as t points to. And that object has two diferent i varaibles.
    so the
    //b.i is t.i,
    No. b.i is Base's i because b is of type Base, and for variables, it's the compile-time referenc type that determine's whose version we see.
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    Dear jverd ,
    Thank you for your explanation.
    I had run the test code as follows, which can proov that your explanation is right.
    class Trebble{
        final int fi = 1;
        static int si = 2;
    
        private void pdo(){
    	System.out.println("Trebble private pdo().");
        }
        final   void fdo(){
    	System.out.println("Trebble final   fdo().");
        }
        static  void sdo(){
    	System.out.println("Trebble static  sdo().");
        }
        void ndo(){
    	System.out.println("Trebble none-p,f,s ndo().");
        }
    }
    class Base extends Trebble{
        final int fi = 10;
        static int si = 20;
    
        private void pdo(){
    	System.out.println("Base private pdo().");
        }
        static  void sdo(){
    	System.out.println("Base static  sdo().");
        }
        void ndo(){
    	System.out.println("Base none-p,f,s ndo().");
        }
    }
    public class Central extends Base{
        final int fi = 100;
        static int si = 200;
    
        private void pdo(){
    	System.out.println("Central private pdo().");
        }
        static  void sdo(){
    	System.out.println("Central static  sdo().");
        }
        void ndo(){
    	System.out.println("Central none-p,f,s ndo().");
        }
        public static void main(String argv[]){
    	Trebble t = new Central();
    	Base b = (Base) t;
    	Central c = new Central();
    
    	System.out.println("t.fi=" + t.fi + " t.si=" + t.si);
    	System.out.println("b.fi=" + b.fi + " b.si=" + b.si);
    	System.out.println("c.fi=" + c.fi + " c.si=" + c.si);
    	t.fdo();  t.sdo();  t.ndo();
    	b.fdo();  b.sdo();  b.ndo();
    	c.pdo();  c.fdo();  c.sdo();  c.ndo();
        }
    }
    And the result is :
    t.fi=1 t.si=2
    b.fi=10 b.si=20
    c.fi=100 c.si=200
    Trebble final fdo().
    Trebble static sdo().
    Central none-p,f,s ndo().
    Trebble final fdo().
    Base static sdo().
    Central none-p,f,s ndo().
    Central private pdo().
    Trebble final fdo().
    Central static sdo().
    Central none-p,f,s ndo().

    I've learned a lot of things from it. Thanks a lot!
This discussion has been closed.