If I have a
class A{ }
class B extends A{ }
and I instantiate a reference variable "var"
A var = new B();
Is "var" then considered to be instantiated as a "B" Object? Where A is the variable reference type and not the Object instantiated for "var".
The object type is still B no matter how you refer to it. That is, the code var.getClass() would return B instead of A, therefore the class of "var" is B.
You could also say that "var" is an instance of A and the code boolean isA = (var instanceof A) would return true, but var.getClass() would not return A.
var is a reference of type A. It points to an object of type B. However, it only knows about the methods that are exposed in A. So, if class B defines a method not present in A, you won't be able to call them from the var reference.
Yes it is going to be successfully casted to A, but you are not going to be able to access any B specific properties from A . See Polymorphism
A and B are both classes. A class is a blueprint, and an object is an instantiation of a class.
However, a variable neither is an object nor holds an object. A variable only holds the location of an object. Therefore, object variables (i.e. non-primitive variables) are called reference variables in Java.
In this specific case, we would say that the variable var is of type A but references an object of type B. Even though var references a B object, it can only access the methods defined in parent A because it is of type A.
A var = new B();
This is a typical example of upcasting in Java.
RHS indicates that object of type B is instantiated in the memory by its default no-args constructor. But you need a reference variable to point to the memory. So in LHS "A var" , var is the reference variable of type A . Since in Java, runtime polymorphism or Dynamic method dispatch is allowed in which call to the overridden method is resolved at runtime rather than compile time and the overridden method is called through the reference variable of the superclass.
So in short. Object of B is instantiated and for achieving runtime polymorphism, it is reference variable of type A.
Hope this understanding will help in discovering your answer.
Related
Let say there is one superclass and two subclasses.
Why Java doesn't allow this scenario:
B -> A (A is superclass of class B)
C -> A
ObjectC c = (ObjectC)(ObjectA)b;
This way I could map common properties from object b to object c.
With (ObjectA)b you have an expression, if evaluated results in a reference of type ObjectA (ObjectA ab = (ObjectA)b). The actual type of the instantiated object (in memory) does not change, only the handle with you hold it (the reference) changes. You cannot cast that reference to ObjectC, because the underlying object instance (originally b) is of type ObjectB.
Let's say we allowed what you wrote in the question ObjectC c = (ObjectC)(ObjectA)b. If you evaluate that you'd get a reference of type ObjectC to the instantiated object b. Let's say ObjectC has a method called run(), but of course, ObjectB does not have to have a run() method. What would happen when you call c.run()?
If you want to map common properties from object b to object c you have to use their common base-class and put common properties there.
Edited with better explanation.
I've been trying to understand casting in Java and how it affects the references. I've come up on this particular example:
public interface A1{
public void foo();
};
public class A2{
public void bar();
public static void main( String[] args )
{
A2 a = new B();
A1 c = (A1)a;
c.foo();
}
};
public class B extends A2 implements A1{
public void foo(){ System.out.println("This is B"); }
}
It prints "This is B", but i'm not sure why. This is how I understand this currently: a is a reference to an object of type A2, but during runtime, it points to a heap object which has the properties of B, but a only "sees" the properties of A2. But the types are already determined during compilation, so the cast tries to cast A2 to A1, which it can't do. Obviously I'm wrong, but I can't figure out why. Any help will be appreciated.
Casting conceptually has two components,
At runtime the JVM ensures that the object is of that type, and
will error with a ClassCastException if it is not
At compile time, it tells the compiler to allow use of the methods/fields of
that class. Note that if the object turns out to not be of that type at runtime, then it will error at runtime.
What casting does not do, is change the type of the actual object at runtime.
In your example you called new B(); that means after casting a reference from B to A1, then the object is still an instance of B. Given that foo() is declared on A1, and B extends foo then the compiler is happy (it passes 2 above). At runtime the JVM will scan B looking for the method of foo, it checks B before A1 because the object was created as type B. It did not change its type since new was called.
Casting checks are always made at runtime (see caveat in next paragraph), your reference points to an object of type B, therefore when you get to the casting bit, the VM will see a reference to an object of B, which can be safely cast. Note that casting doesn't actually change the object, it just ensures that the rest of the code calls methods which are available in the original object.
The caveat is that if it can be seen at compile time that the cast is definitely NOT possible, you do get a compile error.
String foo = "x";
Integer i = (Integer)foo; //throws a compile time error because `String` is final and `Integer` isn't its supertype.
But:
Object foo = "x";
Integer i = (Integer)foo; //this will only throw a runtime exception because from the compiler's point of view, `foo` may or may not be an integer.
The type of the variable that holds the reference has nothing to do with what the method implementations resolve to. In Java, all methods are implicitly virtual, meaning that they get looked up by the actual type of the object (instead of the type of the referring variable) every time. Since c actually points to an instance of B, it's B.foo() that gets called.
Note that this doesn't necessarily hold true for all languages - in C#, for example, methods are only virtual if you explicitly declare them as such and so i its default behavior would match what you were thinking. Java, however, is always virtual.
Reference comes into picture only during compile time or in case of static methods or behaviors.
When you are creating an object, method or behavior will depend on the whose object you have created rather than whose reference you have used.This is called polymorphism.
For example lets take an example of real life entities ---
When you rent a house, you ask a broker to find a house for you, which is your reference.but the person whom house belong, the land-lord is the actual object whom you are requesting. so it is the land-lord who gives you house on rent and not the broker.
I wrote a code to understand runtime polymorphism...
class S{
int i=1;
void m(){
System.out.println("sssssssssssssssssssss");
}
}
public class A extends S{
int i=2;
void m(){
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa");
}
public static void main(String[] args) {
S a=(S)new A();
System.out.println(a.i);
a.m();
}
}
Instance variable are subject to compile time binding, but why down casting of object of A does not meaning here? Means it's invoking A's method not S's method?
S a = (S)new A();
Let's see what you have here:
variable a, of the reference type S;
an instance creation expression yielding an object of type A, where A extends S;
a reference upcast expression, upcasting the above expression into type S;
the assignment of the result of 3. into the variable a.
What you must keep clear in your mind when reading Java is the distinction between:
the type of the object: an object can never change its type. In your example, the object is of type A;
the type of the reference: in your example, you converted a reference initially of type A into a reference of type S. You assigned that reference to a.
When you invoke a method on an object, the actual method invoked does not at all depend on the type of the reference, but only on the type of the object itself. The type of your object is A therefore the method in type A is invoked.
On the other hand, when you access an instance variable, polymorphism does not apply and the type of the reference becomes essential. With a.i you access i declared in the supertype S, and with ((A)a).i you access i from A. Note that the class A posseses two instance variables, both named i, and you can refer to each individually.
A note on terminology
The term "type of a reference" is actually a shorthand for the more correct "type of the expression yielding the reference." It is a purely compile-time artifact: there is no type information associated with a reference at runtime, it's just a bit pattern. Contrast this with the type of the object, which is a purely runtime artifact: the compiler doesn't in general know the type of the object involved in an expression, it only makes assertions about it. When such an assertion fails at runtime, the result is a ClassCastException.
The variable a is a reference of type S to an object whose class is A. When you call m() on that object, you'll always get the version of m() in class A being called, because that's the class of the object, no matter what type of variable is referencing it. That's what polymorphism is about. The version of m() that gets called depends on the class of the object, not the type of the referring expression.
However, this object actually contains two variables called i - one declared in class A and the other in class S. Which one of these you get depends on the type of the referring expression that you use. Since the variable a is of type S, the expression a.i refers to the one that is declared in class S.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java inheritance - please explain
I'm learning Java and I have two questions:
What is the difference between:
A x = new A();
and
A x = new B();
Considering that:
class A
class B extends A
What's the difference between:
A x = new B();
(A)x.run_function();
Let's say that both A and B have the function run_function, which one will be executed ?
The most important difference is between the static and dynamic types of objects and references to objects.
Say B extends A and C extends B.
The dynamic type of an object (the type used in the new) is its actual runtime type: it defines the actual methods that are present for an object.
The static type of an object reference (a variable) is a compile-time type: it defines, or rather declares, which methods can be called on the object the variable references.
The static type of a variable should always be of the same type or a supertype of the dynamic type of the object it references.
So in our example, a variable with static type A can reference objects with dynamic types A, B and C.
A variable with static type B can reference objects with dynamic types B and C.
A variable with static type C can only reference objects with dynamic type C.
Finally, calling a method on an object reference is a subtle and complex interaction between static and dynamic types. (Read the Java Language Spec on method invocation if you don't believe me.)
If both A and B implement a method f() for example, and the static type is A and the dynamic type involved is C for a method invocation, then B.f() will be invoked:
B extends A, C extends B
public A.f() {}
public B.f() {}
A x = new C(); // static type A, dynamic type C
x.f(); // B.f() invoked
Simplifying greatly: first the static types of both receiver (type A) and arguments (no args) are used to decide the best-matching (most specific) method signature for that particular invocation, and this is done at compile-time. Here, this is clearly A.f().
Then, in a second step at runtime, the dynamic type is used to locate the actual implementation of our method signature. We start with type C, but we don't find an implementation of f(), so we move up to B, and there we have a method B.f() that matches the signature of A.f(). So B.f() is invoked.
In our example we say that method B.f() overrides method A.f(). The mechanism of overriding methods in a type hierarchy is called subtype polymorphism.
1. In
A x = new A();
x is an instantiation of A and of type A.
whereas in
A x = new B();
x is an instantiation of B and of type A.
2. The important thing to note here is that (in the second case) if you call x.someMethod(), the method of B will be called, not the method of A (this is called dynamic binding, as opposed to static binding). Furthermore, casting changes only the type, so
A x = new B();
((A)x).run_function(); // Need extra parenthesis!
will still call B's method.
As I said above, you need to include those extra parenthesis since
(A)x.run_function();
is equivalent to
(A)(x.run_function());
Case 1:
You will see difference when you have a method in B which is NOT in A.
When you try to call that method using reference 'x' it won't be visible.
Case 2:
All method calls will be based on object type not reference type due to polymorphism (except static methods)
A x = new B();
In this case B class run_function will be executed.
A x = new A();
In this case A class run_function will be executed.
Furthermore with:
A x = new B()
You will not be able to execute methods that are defined in B and that are not defined in A. However as indicated previously because of polymorphism in Java if you do execute any methods and B' has overridden these methods then it will use B's implementation.
1.What is the difference between: A x = new A();and A x = new B();
The difference is that in the first case, you are instantiating a class of type A. So you will only be able to call methods defined in A. IN the second case, if the same name method exists in both A and B, then the B implementation will be invoked at runtime.
However, in the second case, using reflection, it will also be possible to invoke methods that are defined in Class B and not in Class A.
A x = new B();
(A)x.run_function();Let's say that both A and B have
the function run_function, which one will be executed ?
Remember - Overriding is decided at runtime whereas overloading is decided at compile time.
So the method in class B will be invoked at runtime based on Dynamic Binding.
There is no real difference. Actually for the second case A olds a B object, but B is an A so thats no problem. B in this case behaves like A.
It will call B's run_function()
In Java, an Object can have a runtime type (which is what it was created as) and a casted type (the type you have casted it to be).
I'm wondering what are the proper name for these types. For instance
class A {
}
class B extends A {
}
A a = new B();
a was created as a B however it was declared as an A. What is the proper way of referring to the type of a using each perspective?
I think it's important to distinguish between the object (which exists at execution time, and just has its execution time type) and an expression (such as a variable) which has a compile-time type.
So in this case:
A a = new B();
a is a variable, of type A. Its value at execution time is a reference to an object of type B.
The Java language specification uses "run-time class" (e.g. for the purpose of overriding, as in section 15.12.4.4) for the type of an object. Elsewhere I think it just uses "type" for the type of an expression, meaning the compile-time type.
The Java Language Specification speaks about a variable's declared type, the javadoc of getClass() about an object's runtime class.
Note that there is no such thing as a runtime type in Java; List<String> and List<Integer> are different types, but their instances share the same runtime class.
In this case, A is the reference type while B is the instance type
I would say that you differentiate between the type of the variable/reference and the type of the object. In the case
A a = new B();
the variable/reference would be of type A but the object of type B.
The type of the variable a is A. There's no changing that, since it's a reference. It happens to refer to an object of type B. While you're referring to that B object through an A reference you can only treat it as though it were of type A.
You can later cast it to its more specific type
B b = (B)a;
and use the B methods on that object.
The terminology you are looking for is the Apparent Type and the Actual Type.
A a = new B();
The Apparent Type is A because the compiler only knows that the object is of type A. As such at this time you cannot reference any of the B specific methods.
The Actual Type is B. You are allowed to cast the object (that is change its apparent type) in order to access the B specific methods.
To determine a is object of which class you can use:
/*The java.lang.Object.getClass() method returns the runtime class of an object*/
System.out.println("a is object of: "+a.getClass());
Section 15.5. Expressions and Run-Time Checks differentiates between
the type of an expression
the class of the referenced object
For example,
If the type of an expression is a reference type, then the class of
the referenced object, or even whether the value is a reference to an
object rather than null, is not necessarily known at compile time.
There are a few places in the Java programming language where the
actual class of a referenced object affects program execution in a
manner that cannot be deduced from the type of the expression...
[snip]
An expression whose type is a reference type may be tested using
instanceof to find out whether the class of the object referenced by
the run-time value of the expression
Hence, applying the above language to
A a = new B();
we might say something like
The static type of the expression a is A, despite the fact the value stored in a is a reference to the object of runtime class B.
Personally, I interpret the two concepts in the following manner (but beware I am unsure of its correctness):
static type of the expression is purely syntactic concept existing in source code at compile time for processing by Java compiler
runtime class of the object is actual machine construct existing in machine memory at runtime for processing by Java Virtual Machine