• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inteface Sub Class

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

Can i cast a sibling and execute it without run time. From my understanding its NOT.

How come the following code executes without any runtime exception.

interface DrivingUtilities{};
class FourWheeler implements DrivingUtilities{};
class Car extends FourWheeler{};
class Truck extends FourWheeler{};
class Bus extends FourWheeler{};
class Crane extends FourWheeler{};


public class Test6 {
public static void main(String arg[]) {

DrivingUtilities du;
FourWheeler fw;
Truck myTruck = new Truck();
du =myTruck;
fw = new Crane();
fw = (FourWheeler) du;// casting my Truck and Crane, which are siblings

}

}



Thanks
Chitts
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because they are all legal polymorphic assignments.

Any class that implements DrivingUtilities can obviously be referenced by an Object reference of type DrivingUtilities.

The final line of code is casting a Truck object as a FourWheeler, this is fine since the Truck class extends FourWheeler, you are then assigning the result of the cast to a object reference of type FourWheeler.

By casting or assigning an object to a parent or interface, you do not lose anything from the object (think about how collections work), you are simply changing the way you can interact (call methods) with that object.
 
chitts nathan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Daniel Rhoades.
But still i am not clear.

fw = new Crane();
fw = (FourWheeler) du;// 4

In the line 4, even thought i am assinging the result of the cast to a object reference of type FourWheeler(fw) , fw is assinged to the object Crane, which is the sibling of Truck.

I am trying with another example

class A{}
class B extends A{}
class C extends A{}
public class TestNew1 {
public static void main(String arg[]) {
A a;
B b;
C c;
a = new A();
b = new B();
c = new C();

a =b;
c=(C) a; //IT throws a runtime exception,saying ClassCastException. why its not happening in the Test 6
}
}

I am confussed..
 
Daniel Rhoades
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with your second example is that class C does not extend class A.

You cannot use a parent where its sibling is expected.

For example:

public class Vehicle {
public goForward() {
// Drives forward
}
}

public class Car extends Vehicle {
public openDriversDoor() {
// Opens the drivers side door
}
}

Vehicle car = new Car(); // This is fine since car extends vehicle
car.goForward(); // Also fine since this method is inherited
car.openDriversDoor(); // BAD, the reference type doesn't understand this method

Car car = new Vehicle(); // BAD, the reference type expects the openDriversDoor() method, but the object (Vehicle) does not have a method with that signature

The reason your source compiles but then fails at runtime, is because object casting is only checked at runtime.

In my example note that a car can do anything a vehicle can do, but not the other way around - this makes sense if you look at the fact that car has more methods than vehicle.

Hope that makes sense!
 
Daniel Rhoades
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chitts nathan:

fw = new Crane();
fw = (FourWheeler) du;// 4

In the line 4, even thought i am assinging the result of the cast to a object reference of type FourWheeler(fw) , fw is assinged to the object Crane, which is the sibling of Truck.



Be sure to understand the difference of a reference and an object:

ReferenceType something = new Object();

Think of a reference type as a contract between you and the compiler: anything I assign to fw IS A FourWheeler.

Crane extends FourWheeler, Truck extends FourWheeler, therefore Crane IS A FourWheeler, Truck IS A FourWheeler, so the following is legal:

FourWheeler fourw = new FourWheeler();
FourWheeler crane = new Crane();
FourWheeler truck = new Truck();
 
chitts nathan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Daniel Rhoades. That makes sense.
Thanks for your detailed explanations.. :-)
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't read the whole post , but my notes on casting :
--------------------------------------------------------

1] you can assign sub class object to super class reference without any casting ....

2] you can assign super class object to sub class reference with explicit cast , but you will get ClassCastException , not the compiler error ...

3] you can't assign an object to a reference , if they are not related at all ( no parent-child relationship ) ...


hope everything is right ...
 
Evacuate the building! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic