0

Suppose I have 3 classes:

  • The super class is: Animal
  • There is 1 subclass Dog that inherits Animal
  • There is 1 subclass Cat that inherits Animal

Now if I do this: Animal a = new Dog();

Does the object Dog that a points to get casted to Animal?

If yes then does this cast both the variable and the object to dog?

((Dog)a).bark(); // bark() is specific for the Dog

Also what are the rules for casting these reference types in Java?

1 Answer 1

1

I will try to describe what happens in the background, in the hopes that it will clear some things. Lets break this declaration & definition into parts.

  1. 'a' is declared as an Animal.
  2. 'a' is defined as a Dog.
  3. Dog extends Animal, therefore, animal methods (including constructor) get defined.
  4. Dog's methods get defined, whilst overriding any method that was defined by the parent class Animal) and overshadowing member variables of the parent class.

When looking at 'a', you are using the 'Animal' perspective and for that reason you are not capable of using any methods (or fields) declared only by sub-classes of Animal. In order to "gain" that perspective you can downcast from Animal to Dog exactly as you did. Keep in mind that downcasting is not safe whereas upcasting is.

For example:

Animal a = new Cat(); // cats can mew
Animal b = new Dog(); // dogs can bark
(Dog)b.bark() // Unsafe but compiles and works.
(Dog)a.bark() // Unsafe but compiles and throws a run time exception. 

In general, a good practice would be to create an abstract class for an Animal and then override some of it's methods with subclasses. For example:

public abstract class Animal {
    abstract void makeNoise();
}


public class Dog extends Animal {
  void makeNoise(){
    System.out.println("bark");
  }
}


public class Cat extends Animal {
  void makeNoise(){
    System.out.println("mew");
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Do you know how do I imagine it? When I create an object of Dog there are 2 objects created accessible by one (Animal, Dog). The dog contains the methods bark() and the animal contains the specific stuff for the animal. When I do Animal a = new Dog(); Since the reference type is animal the Dog part of the object doesn't work, only the Animal part of the object does. Then when I do a cast of the reference variable to Dog the Dog part of the object is enabled and I can do the things specific to Dog. Is it a good way to imagine it? Or It's a wrong way?
When you create a dog, only one object is created. It is both, a dog and an animal. Whether you treat it as a dog or as an animal is up to you. Creating a new dog creates an animal which is a dog. Choosing whether you want to view in in the animal perspective (which is more abstract) or the dog perspective is up to you and depends on your needs. You can imagine the 'cast' being special glasses that allow you to choose how you want to see your object. Either way, it will be one and the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.