Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance of operator example

 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Vehicle {

String name;
Vehicle() {
name = "Vehicle";
}
}

class HeavyVehicle extends Vehicle {

HeavyVehicle() {
name = "HeavyVehicle";
}
}

class Truck extends HeavyVehicle {

Truck() {
name = "Truck";
}
}

class LightVehicle extends Vehicle {

LightVehicle() {
name = "LightVehicle";
}
}

public class InstanceOfExample {

static boolean result;
static HeavyVehicle hV = new HeavyVehicle();
static Truck T = new Truck();
static HeavyVehicle hv2 = null;
public static void main(String[] args) {
result = hV instanceof HeavyVehicle;
System.out.print("hV is an HeavyVehicle: " + result + "\n");
result = T instanceof HeavyVehicle;
System.out.print("T is an HeavyVehicle: " + result + "\n");
result = hV instanceof Truck;
System.out.print("hV is a Truck: " + result + "\n");
result = hv2 instanceof HeavyVehicle;
System.out.print("hv2 is an HeavyVehicle: " + result + "\n");
hV = T; //Sucessful Cast form child to parent
T = (Truck) hV; //Sucessful Explicit Cast form parent to child
}
}
I was going through above example from link

http://www.javabeginner.com/learn-java/java-object-typecasting

got output like
hV is an HeavyVehicle: true
T is an HeavyVehicle: true
hV is a Truck: false
hv2 is an HeavyVehicle: false


I have not understood the output clearly.


Any ideas, resources,sample code,links, highly appreciated. thanks in advance.



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ShowSomeEffort.

Explain in more detail why you don't understand this code and its output.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And UseCodeTags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic