I have nothing clear about casting reference variables in Java.
I have two classes A and B, I have a super class. If I have two objects, then print statement:
A = new A (); // Superclubs BB = new B (); // subclass System.out.println (A) B);
So what is happening when the println method is executed
I know that because there is a subclass of BA, I have the following permission to cast Given ?:
A2 = (A) B;
I also know that when the println takes a reference variable as the argument, then the class's toString () method, which has created an object-argument, is called (inherent) is. This is because the method is looking for the argument of println () type string, and the toString () method represents the object as a string. And even if we do not write string (), then the method applies - as such, the following two statements are equivalent:
System.out.println (b ); System.out.println (b.toString ()); So, my question is: when we have the inherent action taken on System.out.println (A) B);
?
I think the type of reference variable B is automatically changed from B to A. The variable is still pointing to the same object -
BB = new B ();
But the type of B will change now is this correct? Another question: Even though I have changed the type of type to super-class, is there any way to override the subclass, and not the superclass?
Thank you very much.
There are println (...)
in < Code> PrintStream class announcements (which is the type of system.out
).
Two of them are:
zero println (string x) zero println (object x)
when you call Println ((A) B) chooses to call the
compiler println (object)
because A
is not string
(Or otherwise by some other type, supports println
). When you call println (b.toString ())
, the compiler chooses println (string)
because you're passing a string.
In your case, b
has no effect with a
because println () has no declaration for A or B type. But the artist will still happen (because you had asked for it), or it may be because the compiler will not remove it because it knows that it is redundant and it can not be unsuccessful and it has no effect.
It is not idiotic to write:
AA = (A) B;
Because it is unnecessary because there is a subclass of BA. It may be that the compiler removes artists (which is check regardless of any particular type of object, it is never to be changed.)
Once When an object is created, its never changes does not change it is always a b:
class b expands / applies {...} BB = new B (); // Build a B AA = B; // Assign an A to an A, this superclass AA = (A) B / as shown above that B. A to see B. (unnecessary, can be adapted far). BB = A; // syntax error, bb = (b) will not compile A / B // If there is a type B or not, then in the last case, variable b
, B
This is a subclass of A
, maybe an example of someone B
and the artist will succeed. Or it may be that some other classes have an example of extending / tool / a
and not a b
and you will get a ClassCastException
.
So when the Type B object always preserves its identity (it is "B") then any (instance-) methods called on that object always call the implementation of B, regardless of The variable through which you can reach the object was declared as A or B.
Remember, you can only call the methods that are declared in the class that the variable is defined.
For example, if B declares a method b_only ()
, then the compiler will not allow you to write a.b_only ()
; You can write ((b) A). B_only ()
However.
Comments
Post a Comment