• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

About Runnable and Serializable

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello Can some one explain me why this code is compiling without errors ?
Thanks for your help .

import java.io.Serializable;

class B extends A{}
class C extends B{}

public class A {
public static void main(String[] args) {

A a = new B();
B b =new B() ;
C c = new C() ;

Runnable r1 = (Runnable)a ;
Serializable r2 = (Serializable)a ;
Serializable s2 = (Serializable)c ;
}

}
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we explicitly cast to an interface the compiler trusts us. You can cast it to any interface, try Set or Map instead of Runnable but not to a class (eg String). This compiles but will throw a run-time exception. If you remove the cast and just try

Runnable r1 = a ;

This will give you a compile time error. Experts could give better explanation.
 
Sheriff
Posts: 28329
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not quite that the compiler trusts us. The actual reason is that something assigned to a variable of type A could actually be an object of some subclass of A which does implement Serializable, or Runnable, or any other interface. Since that might happen, the compiler can't call it an error, although it might fail at run time with a ClassCastException.

However if A is a final class, then the compiler knows that this situation can't ever happen -- there can't be any subclasses of A -- and in that situation it will flag the cast to an interface as an error. Try setting up an example like that and see what happens.
 
Alain Narcisse
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer and what about this ( also compile without errors ) .

A a = new B();
B b =new B() ;
C c = new C() ;

Runnable r1 = (Runnable)a ;
Serializable r2 = (Serializable)a ;
Serializable s2 = (Serializable)c ;

// ?
A a1 = (A) r1 ;
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul for the awesome explanation. I tried the following code:



Since an implementation class of an interface can extend from a class, the compiler allows class casting as well. That will explain the A a1 = (A) r1 ;
 
What's gotten into you? Could it be this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic