Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Could u tell me why in this SCJP question?

843789
Member Posts: 46,655 Green Ribbon
What will happen when you attempt to compile and run the following code?
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!
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
-
The value of i in class B is 100.
-
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. -
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. -
Trebble t = new Central(); //Now the i value is 99There 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,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.
pointing to t.so theNo. 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.
//b.i is t.i, -
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.