sir_thursday
sir_thursday

Reputation: 5419

What is the point of casting an instance to a certain type?

I am a bit confused on the purpose of declaring instances as specific types. For example,

Integer intOb1 = new Integer(3);
Object intOb2 = new Integer(4);

I know the type of intOb1 is Integer, and the type of intOb2 is Object, but what does declaring intOb2 as Object allow one to do? Use methods in Object? Can't you already use those methods as an Integer? Or is the main purpose just to be able to "treat" intOb2 as an Object?

As you can see, I'm befuddled.

Upvotes: 2

Views: 488

Answers (3)

neo
neo

Reputation: 1074

You can always use method of the "Object" class from object of any type of class because all classes are subclasses of "Object"

Instead, you need to understand this why anyone create object of type class Object:

Type casting generally required when you want to write some method/functionality and you are not sure which type of object may come as input. One of the most common example in JDK is equals method or compare method in comparator.

// Object equals method - written for such generic use.
boolean     equals(Object obj) {
  //Here you might want to check which is other type of object.
  if (!obj instanceof CurrentClass) {
      return false;
  }
  //Additional logic required to check equality of object.
}

Similar case was also there for the Collection library earlier(JDK 1.4) but JDK 1.5 introduced new feature called generic.

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61530

This actual isn't called casting this is an example of polymorphism. Which allows variables to take on different types based on their inheritance relationships with other classes.

For example, suppose you were writing a program to simulate a zoo. You would have a class called Zoo and a class called Animal. There are also several classes that extend from the Animal class: Lion, Zebra, and Elephant.

It would be extremely useful to keep all of these objects grouped together in a single list but since they are of different types, i.e: Lion, Zebra, and Elephant, you can't store them in a single list, you would have to maintain a separate list for each type of animal. This is where polymorphism comes into play.

Since each of the classes Lion, Zebra, and Elephant all extend from the Animal class we can simply store them in a list of type Animal.

code example:

public class Zoo
{
   private List<Animal> animals; 

   public Zoo()
   {
      this.animals = new ArrayList<>();
   }

   //notice this method takes an Animal object as a parameter
   public void add(Animal a)
   {
      this.animals.add(a);
   }
}

public abstract class Animal
{
   private String name;
   private String type;

   public Animal(String name, String type)
   {
      this.name = name;
      this.type = type;
   }

   //all subclasses must implement this method
   public abstract void speak();
}

public class Lion extends Animal
{
   private String animalType = "Lion";

   public Lion(String name)
   {
      super(name, animalType);
   }

   public void speak()
   {
      System.out.println("ROAR");
   }
}

//....etc for the other two animals

public class TestZoo
{
   public static void main(String[] args)
   {
      Zoo myZoo  = new Zoo();
      Lion l     = new Lion("Larry");
      Elephant e = new Elephant("Eli");
      Zebra    z = new Zebra("Zayne");

      myZoo.add(l);  //<-- note that here I don't pass Animal objects to the 
      myZoo.add(e);  //    add method but subclasses of Animal
      myZoo.add(z);
   }
}

Hope this helps, even with a silly example.

Upvotes: 2

Jack
Jack

Reputation: 133609

When Object is involved you never get anything useful, since it's a generic type which doesn't help anywhere.

But think about a different situation:

List<String> list = new ArrayList<String>();

This means something specific: list is an ArrayList and declaring it just the generic List collection enforces the fact that throughout the code you won't use any ArrayList specific method.

This effectively allows you to state that you won't rely on any specific implementation, you will later be allowed to switch ArrayList<String>() to, for example, LinkedList<String>() without having to modify anything else.

Upvotes: 2

Related Questions