Java Compiler and Interface casting - java

I'm looking at casting in Java, and specifically casting with interfaces. Suppose I have an interface I, that defines a method jump, i.e.:
interface I{
public void jump();
}
Additionally suppose I have 3 other classes, A, B and C. A implements I. B does not. However, C extends B and implements I.
class A implements I {
public void jump(){
System.out.print("A is jumping");
}
}
class B { }
class C extends B implements I {
public void jump(){
System.out.print("C is jumping");
}
}
Now if I try to assign an object of type A to an I, there is no problem and I do not even need a cast. I.e.:
I i = new A();
is OK no need to cast.
Presumably this is because the compiler knows that A implements I. Furthermore, if I try to this:
A mya = new A();
I i = mya;
No problems, even though mya could be referencing a subclass of A. But AFAIK that's OK since the compiler knows that every sub class of A must implicitly implement the jump method and therefore the interface A.
However, if I try to assign an object of type B to an I, then I do need a cast. e.g.
B myb = new B();
I i = (I)myb;
Presumably this is because the compiler knows that B does not implement I. However, since B could refer to a subclass which does implement I, then you are allowed to cast to I. So far so good.
Now here's my question: If I want to assign an object of type B that refers to an object of type C (which implements I) to an I then we require a cast. Why is that? E.g.
B b = new C();
I myI = b; //Won't compile this needs a cast
I myI = (C)b; // This will compile
myI = (I)b; // This will also compile
Why doesn't the compiler understand that B is referring to a C that implements I?
Presumably it's to do with the fact that B could refer to a B which doesn't actually implement I but why is it that the compiler doesn't know that? Presumably the compiler is limited to the information that is only available on each line? It can't run through your program and see that b is in fact pointing to a C? If that is correct can someone point me in the direction of some literature on how Java compilers work and what exactly are their limitations?

Presumably this is because the compiler knows that B does not implement I, however since B could refer to a subclass which does implement I then you are allowed to cast to I.
Not exactly. The compiler will let you cast B however you want. That doesn't mean you won't get a ClassCastException at runtime. (Your code will work this time, but the compiler won't prevent you from making bad casts.)
Why doesn't the compiler understand that B is referring to a C that implements I? Presumably it's to do with the fact that B could refer to a B which doesn't actually implement I but why is it that the compiler doesn't know that.
Because you told it to treat the object as a B when you declared B b = new C();
Presumably the compiler is limited to the information that is only available on each line? It can't run through your program and see that b is in fact pointing to a c?
It has access to the rest of the code. It's just doing what you told it to do: treat b as an object of class B.

In this particular case, compiler has the ability to determine that b actually refers to an object of C. However language creators chose not to make the the compiler so smart.
In practical scenario, actual object which b refers to, would not be so localized and so compile time determinable. So it was chosen not to make the compiler be over-smart which does not solve practical scenarios.

Related

Java inheritance casting runtime error vs compiler error

The Problem
Consider the code below. B inherits from A, and none of them inherit from String.
I'd like to know the answers to the following questions:
why does the first first cast, B b1 = (B) a1;, produce a runtime error?
why does the first second cast, String b1 = (String) a1;, produce a compilation error?
Why is there a difference? Why doesn't the compiler see the problem in the first case?
The Code
public class Main {
public static void main(String[] args) {
A a1 = new A();
B b1 = (B) a1;
String b1 = (String) a1;
}
}
with class A:
public class A {}
and class B:
public class B extends A {}
A variable of type A could have been assigned an instance of B, because a B is an A. Eg a Dog is an Animal, so a box labelled “Animal” could contain a dog.
But a variable of type A cannot have been assigned a String. Eg A box labelled “Animal” will not contain a Brick.
You may be asking yourself why the compiler doesn’t complain when we can see that the code will clearly fail - there’s no way the variable is a B; it’s an A!
The compiler looks only at the type of the variable when making its checks. It doesn’t examine what code came before. Although your example is simple, checking what a variable actually contains would be an impossible task in the general case.
why does the first first cast, B b1 = (B) a1;, produce a runtime error?
Variables of type A can store instances of B. However, not all instances of A are instances of B.
An A is not a B, but a B is an A. (Like, not all animals are dogs, but dogs are animals).
why does the first second cast, String b1 = (String) a1;, produce a compilation error?
A is not a supertype of String, and String is not a supertype of B.
Why is there a difference? Why doesn't the compiler see the problem in the first case?
Because variables of type A can store instances of B; but variables of type A can never store instances of String.
A variable of type A could in fact be of type B as B extends A. But a variable of type A can never be of type String. That's why the compiler can catch the cast to String, but not the cast to B.
why does the first first cast, B b1 = (B) a1;, produce a runtime error?
Because a1 is an instance of A, but is not compatible with B. Specifically, new A() creates an object that is not compatible with subclasses of A. If the runtime class (i.e., the class with which new was called) of the object is not the same as or a subclass of the target class, casting to that target class will fail at runtime. This is simply because the child class has nothing to do with that object.
why does the first second cast, String b1 = (String) a1;, produce a compilation error?
Even if the actual casting happens at runtime, the compiler performs type checks and prevents pointless operations like this. For this scenario, casting an A object to String is nonsense and the compiler can detect it: there is no relationship between String and A, and the compiler knows what class is a child of what other class. The compiler knows that there is no way in Java for a1 to be an instance of String or of a subclass of String, and that's because String is not a parent of A, the declared type of a1. There are exceptions to this, such as when the cast is begin made to an interface.
Why is there a difference? Why doesn't the compiler see the problem in the first case?
The compiler only validates type casts based on static types (the type of the variable or of the expression). It doesn't look at the runtime class, which of course isn't available until runtime when the object is actually created. When it can determine with certainty that the cast can't possibly be valid (such as in the second case), it will fail. In the first case, casting from A to B passes compilation because the declared types are compatible (i.e., an A object can possibly be an instance of B, and the compiler leaves it for the runtime to check the actual object). In the second case, the compiler knows that an A object can never be an instance of String (because String is nowhere in A's type hierarchy, and this won't change at runtime)
The class hierarchy diagram for the class A would be:
Object -> A -> B (Note that every class extends Object)
B b1 = (B) a1;
The above line compiles because B extends A and hence the compiler sees it as a valid downcast. The java compiler only checks whether it is possible for an object of type A to be of type B, by checking the class hierarchy of B (whether B extends A directly or indirectly). It doesn't check the actual type of the object A at this point. It wasn't implemented this way since it would add a lot of complexity in the compiler. Also if an object is being downcast (to call some specific sub class method perhaps), then the responsibility is on the programmer to be aware of the specific type of the object. In this example since a1 can't be cast to type B, it will be detected by the JVM at runtime.
String b1 = (String) a1;
In this case, the class String is nowhere in the class hierarchy diagram of A. Therefore it can be detected at compile time that this is an invalid cast.

Understanding Multiple Casting in Java

Skip to the last sentence if you want to read the question right away.
Suppose we have an Interface and three classes:
interface I{}
class A implements I{}
class B extends A {}
And the following declarations:
A a = new A();
B b = new B();
Now, there's the classic way of casting which allows me to cast a reference of type A (the parent class) to an object of type B (the child class) like this:
a = b; //here a is no longer pointing at object A, and is now pointing at the same object b is pointing at.
b = (B) a; // the casting is now accepted by the compiler and during runtime as well.
Here where lies the problem though. Every time I see a line of code with multiple casting, I fail to read it (literally) and, as a result, I can't understand what it means.
For instance, let's say we have this line of code:
a = (B)(I)b;
How would you read this one? a is a reference to an object of type A, and it is being assigned the value of an object of type B (first cast from the left). But wait a minute, there's also another cast (I) preceding b. So what do we have here? Is it an interface being cast as a (B) object? or is it a b being cast as an interface which is also being cast as a (B)?
I tried to break it down to avoid confusion:
I temp = (I) b;// first line
a = (B) temp;// second line
So, first, since b is an I (because it extends A which implements I), "first line" is accepted by the compiler and during runtime.
"Second line" though, we have a reference to an object A being assigned a value of type B. At first glance, there's nothing wrong with it. But then I realized I is not an A nor is it a B, and even though the cast in "second line" can dupe the compiler into believing it's an object of type B, it shouldn't be accepted at runtime.
So the main question that I would like an answer to is how do I interpret the following line:
a = (B)(I)b;
Reality or The answer you don't want
The real problem here is that a careless goofball wrote crappy code.
The real solution is; either don't write crappy code or fix the code.
Lets keep being a goofball or The answer you seem to want
There are two types of casting in java; up-casting and down-casting.
Up-casting is when you cast an object of type A to be an instance of interface I; you are casting "up" the inheritance tree.
For example:
A aObject = new A();
I iObject = (I)aObject; // Up casting.
The benefit of up-casting is that the compiler is able to determine, at compile time, if the cast is legal.
Down-casting is when you cast an object of type I to be an object of type A; you are casting "down" the inheritance tree.
For example:
A aObject;
I iObject = new A();
aObject = (A)iObject;
The compiler does not know, at compile time, if down-casting will succeed.
Because of this, a down-cast may throw an exception at runtime.
Your confusing code: a = (B)(I)b; is an example of both up-casting (safe) and down-casting (not safe).
As a bonus, the casting is in no way required.
It is always safe to assign a B object directly to an A reference because the B class extends the A class.
Addressing: "careless goofball" seems like strong language.
It is not strong language, it is the nicest way to describe the cause your situation.
In truth, somebody who writes code like that should be terminated (optionally, get them hired by one of your competitors).
Based on the Java language grammar, the statement
a = (B)(I)b;
might be better visualized like this:
a =
// outer CastExpression
(B)(
// with its UnaryExpression being another CastExpression
(I)(b)
);
That is, it casts b to I, then casts that to B, then assigns that to an A variable.
However, it doesn't look like either of these casts are necessary. If b is an instance of B, it is also an instance of A and I.
a = (B)(I)b;
Cast b to I, and then cast it to B. Presumably since you can't cast b to B directly.
Now there aren't really good situations to use this, since casting even a single time should be avoided if possible. However if you want something like
String s = (String) myHashMap;
to compile, you need to upcast to prevent the compiler from disallowing an obviously illegal cast:
String s = (String) (Object) myHashMap;
Naturally if your myHashMap isn't null, this will cause a runtime ClassCastException, but unlike the earlier example it will compile.
Maybe you don't get the point that, even if cast to I or A, b remains an Object of type B, it doesn't loose its nature. Think of casting a way to say to java 'see my object as an object of type I'. Then if type B it is also of type A.
So the instruction tells java 'use my object as it is of type I and immediatly after use it as type B, which, by declaration, is also of type A. So no compile or runtime errors.
Then we can see.that it looks also mostly unuseful and ugly..

Upcasting in Java for non primitive types

Let us consider we have two classes A and B. B is a sub class for A because B extends A. If We create an instance of A Class and assign that in to a A type will contains all the properties of A. Similarly when I create an Instance of B and assign it to B type will get all the properties of B along with properties of A because it is inheriting from A. According to above lines instance of A contains properties a few as compared to properties contains to instance B. That means Instance of B is Bigger than Instance of A as casting should be explicit when narrowing implicit when widening. According to my theory Instance of B is bigger we are trying to store it in A type we need conversion.
A a1=new (A)B();
The above conversion is taking place implicitly. But my question is how it is implicit, Instance of B is bigger we are trying to convert that to small type which is A. How this is possible???
Answer me with examples thank you in advance.
You are thinking in terms of object size, but in Java, non primitive types are never contained, only referred to. Thus, your code is casting the result of new B(), which is of type "reference to B", to type " reference to A". Since all references are the same size, no data is lost in the cast.
So, I really don't understand your question. I just think you are confused about what happens to the class B members when a upcast to his super class is made. In that case, you ended up with a instance of A wich means that Object type is A and non of B stored data will remain.
In Java, with
B b = new B();
A a = b;
one defines references b and a. Under the hood, references are implemented with pointers, and thus, all references are the same size. Of course, an instance of a B might indeed require more memory than an instance of A (I take it, this is what you mean by "bigger").
By the way, in C++ this is not the case.
B b();
does define an object, not a reference, and therefore
A a = b;
in C++ is indeed not allowed.
Think about this:
class Animal{
public void eat(){}
}
class Monkey extends Animal{
public void climbTree(){}
}
I can now do this:
Animal someAnimal = new Monkey(); //This is ok. (Create a Monkey and identify is as an Animal)
someAnimal.eat(); //This is ok too. (All Animal objects can eat)
someAnimal.climbTree(); //Not ok. Java sees someAnimal as a general Animal, not a Monkey yet.
From the above example, someAnimal is a Monkey object which is stored within a variable of higher hierarchy (Animal).
The object itself is perceived as the a more general class (The Animal class) and I don't think an implicit casting is done here since all Monkeys are already Animals (but not the other way round).
Explicit casting can be done when you want to let the compiler knows that the object actually belongs to a more specific class. For example:
((Monkey)someAnimal).climbTree(); //This is ok. Inform Java someAnimal is actually a Monkey which knows how to climb.
Example :
Class Employee {
private String name;
private double Salary;
//getter & setter
//mutators to increase salary etc;
}
class Manager extends Employee {
private double bonus;
//inherits methods from superclass
// sub class specific methods
}
Employee e = new Manager(); //is fine as manager is also an Employee...
The prefixes super and sub come from the language of sets used in theoretical computer science and mathematics. The set of all employees contains the set of all managers, and thus is said to be a superset of the set of managers. Or, to put it another way, the set of all managers is a subset of the set of all employees.
~ From core java series
hope this helps...
The cast isn't actually implicit like you're saying. What is actually happening is this:
B b1 = new B();
A a1 = (A)b;
The (A) is an explicit cast, what's more important is to stop considering the size of things in the sense that the size of B is different from the size of A. This can be an implicit assignment because B IS-A A, so using A as an interface for B is completely safe, because we know that B has at least the same members as defined by A.
So the perfectly safe (and not erroneous) method of doing this is simple:
A a1 = new B();
Done!

Type Casting between unrelated classes in JAVA

Let there be two classes defined as follows:
Class A{
int a;
}
Class B{
int b;
}
A and B are two unrelated classes. Is there anyway I can cast an object of A to B? If yes, please explain the various options I have.
You can do
Object b = new B();
A a = (A) b;
But this will throw a ClassCastException at runtime.
Instead you can use a copy Constructor
class B {
public B(A a) { ... }
public static B toB(A a) { return new B(a); }
}
And then you can do
B b = new B(a);
or
B b = toB(a);
You can do an upcast to a common super type, followed by a downcast
(Dog)(Pet)aCat
Of course, Object is the supertype of any type, so we can use it to cast between any 2 types
(Apple)(Object)aCat
Usually, this makes no sense, and it will cause runtime exception. But it may be helpful in some generic cases. For example, Supplier<Integer> and Supplier<number> are "unrelated"; as a matter of fact, they are mutually exclusive, i.e. the intersection is empty, or, no object can belong to the two types at the same time. Nevertheless, we may want to cast Supplier<Integer> to Supplier<Number>, due to lack of variance in Java, combined with the existence of erasure. See this case study.
You can't. That would break a fundamental principle of object-oriented programming.
Though JAVA allows you to type cast object across unrelated classes i.e. which are not in class hierarchy. This is allowed because of the below case
String s = (String)list.get(1);
Well list can certainly contain different type of object and while calling such a method it could return String as well.
Coming to your question you will succeed in typecasting but at runtime you will get ClassCastException .
You can type cast object B to type A iff B IS-A A that means B is a subtype of A. If both are not in a hierarchy then type casting them does not make sense. just like you can not type cast an Animal type to Furniture type.

Upcasting in java

In Java, suppose I have 3 classes, C extends from B which extends from A.
class X {
interface A {}
interface B extends A {}
interface C extends B {}
void f(A a) {}
void test() {
C c = new C()
B b = (B) c;
f(b);
}
}
If I do something like this as shown in test() above:
C c = new C()
B b = (B) c;
f(b);
f() accepts b as type C since C and B both extend from A. I wanted f() to receive b as type B and not type C.
Is there anyway to force this upcasting?
f() will always receive something typed as A (despite under the covers it's actually a B or C, and can be downcast appropriately).
You can define an additional f() thus
f(B b);
and if necessary
f(C c);
and the correct one will be called depending on the class of the argument. i.e. the compiler determines which function is called depending on the type of the argument. This is different from a dynamic dispatch (or polymorphism) which would occur at runtime.
Note that your cast in the question is redundant. You can write:
C c = new C()
B b = c;
f(b);
since C extends from B, C is a B.
You seem to be confused about the difference between compile time type and runtime type.
You are creating an object (pointed at by the references c and b) of type C, and it will stay a C, because it is impossible to change an object's runtime type and therefore behaviour; by casting you can merely change its compile time type, which affects how the compiler treats it.
Can you give some more information about the concrete problem you're trying to solve? Most likely there is a way to achieve your goal by changing the design.
I wanted f to receive b as type B and not type C.
A C is a B is an A.
Inside of f, f only sees the A part of its parameter a If f calls a public A function that is overridden in C, the C override is called.
That's how virtual functions work. The idea being, the object referred to is really a C, so it should exhibit C behavior. If you want B behavior, pass a B instance, not a C instance.
If 'C' and "B' should have the same behavior, don't ovveride that behavior in C.
Why do you want to do this?
Your question does not make sense. What do you mean by "f accepts b as type C"?
f accepts b as type A, since the method signature says "A". If you invoke methods on b, they will be invoked on C if C overrides them. But this is standard behaviour in Java (all methods are like virtual methods in C++), and there's no way to change it.
Maybe you can describe your actual problem, then we might be able to help.
Your issue can be resolved by understanding the difference between REFERENCE type and INSTANCE type. When you cast you C object as a B, you only change the reference type - the actual instance of the object is still a C object - this should be quite obvious if you look at your object in debug mode. Changing the reference type only impacts how the compiler handles/perceives the code - runtime behaviour should not be affected. Any method call on an C object will always invoke C's method (regardless of whether or not the object has been casted to something else). If you overode a method from B and want to invoke B's version of the method, then you will need to create a B instance.
The fact that you can pass any subclass of A as a parameter of f(A a) is inherent to OO in Java, there's no way you can go around this. If C extends A, you can always use it where an A is expected.
you can use reflection to check if the parameter is of class A:
public void f(A a) {
if (a.getClass() == A.class) {
// ok, go on
}
else {
System.err.println("The parameter is not of class A!");
}
}
don't know if that's what you want, but it may be helpful.
The fact your code has a reference to an A doesn't mean the object being pointed too is an A. If it's a a C it remains a C. The reference in your source only limits the available methods in your source. Casting is only necessary because sometimes one needs a method on a different type and we need to trick the compiler so it let's us start using methods on the castes to type. Naturally the cast can fail at runtime if the attempt us invalid.
In upcasting and downcasting the object first upcast then downcastenter
class A
{
}
class B extends A
{
}
Class M
{
psvm(String ar[])
{
A a1= new B();
B b2=(B)a1;
}

Categories