

Generics in fact make unnecessary theĭowncasts from Object. Note: The introduction of Generics in Java makes unnecessary So, downcasting helps us to solve a problem, but produces another problem. Of course we could use instanceOf, but doing this would mean that we are doing a work Now the method compatible will try to make tuky bark!!! The problem now is that the type system will not prevent us to take a Dog fido So, we are forced to use the downcast in the redefined method: P.bark() we get a type error detected statically, since p is considered to belong So if in the body of the redefined method we write The redefined method, by the Java typing rules, cannot take a DogĪs argument, but an Animal. In case two dogs are compatible, they also bark. Now let us assume we have also a class Dog wich is a subclass of AnimalĪnd we wish to redefine the method compatible in order that, This method checks whether two animals can stay together Let us assume to have a class Animal with the following binary method Of the domain and codomain of a method cannot change. Well, for instance in order to overcome some problems due to the fact that in overridden methods the type Question 3: If the downcast is dangerous, why does Java use it? In fact, for well-typed programs in Java, rule (T-SCast) is needed only to type some expressions We have to consider however that if we take a program well-typed in the type system without (T-SCast), such a program is also well-typed in the type system that uses (T-SCast). The type system of FJ is not precisely the one of Java: it contains Programs that do not use downcasts at all are type-safe, i.e. Get stuck, but the cause of that lies only in the use of downcasts. So now we know that well-typed Java programs are potentially type-unsafe, i.e. We extend the type system of FJ with rule (T-SCast) and this allows the Subject Reduction property to hold. Just one step of evaluation, that is (A)new B(), is not well-typed. In fact the expression (A)(Object)new B() is well-typed in Java, but the expression obtained after In order to prove Th.2.4.3 it is necessary to prove the Subject Reduction property (Th.2.4.1).īut the subject reduction property cannot be proved for the type system of Java, and in particular for the type system of FJ without rule (T-SCast) !! Run-time error (1), (2) or (3) can occur, it is enough to prove that reduction preserves So, in order to show that during the evaluation of a safe expression no

(2) a wrong field selection ( nonexistent-field) or This means that the evaluation of a safe expression is not blocked because of New C 0( e).m( d) with m not among the methods of the classĭifferent from the number of aruments required by m.New C 0( e).f with f not among the fields of class C 0.(C)New C 0( e) with C 0 not a subtype of C.That no safe expression can contain subexpressions of the following form In the theorems 2.4.2 and 2.4.4 it is proved Rules (T-DCast) and (T-SCast) are not used. Question 2: Are there other run-time errors which can occur during the execution ofĪn expression is called safe if it is well-typed and Question 1: How can we guarantee a well-typed Java program not to produce this sort of type errors? Of the sort a type system should actually prevent. Then the evaluation of the following well-typed expression results in a run-time error In fact it cannot prevent us to write a program producing the following type error, as showed in : The type system of Java (essentially the type system of FJ without rule (T-SCast) ) is type-unsafe. In the authors describe the type system of Java, but extends it with the Is is only allowed if the object was actually created as a type B.About rule (T-SCast) in FJ and downcasting in Java transforming an object of type A to type B. What you are asking about is down-casting which is making an object be of a more specific type, i.e. When you use an object of type B in place of A you actually do a up-cast, meaning you make the object of type B look like an object of type A (a less specific class type). If you have a class hierarchy where class B extends class A (B is a specialization of A) you can use B in the place of A since B supports all the operations that B does. In the working example, above, the dogwood references a Dogma, so you can downcast the Dogma. You also cannot downcast a Dog to a Dogma. You cannot upcast a Dogmatic to a Dog and then downcast it to a Dogma. 1.7K Training / Learning / Certificationĭog dogwood = new Dogma() // a Dogma object is upcast to a Dogĭogma bush = (Dogma) dogwood // a Dogma object is downcast from a Dog.165.3K Java EE (Java Enterprise Edition).7.9K Oracle Database Express Edition (XE).3.8K Java and JavaScript in the Database.
