Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Inheritance in Java Inheritance in Java Casting Instances

((Animal) object.makeSound())

This is the first time I ever see the code to be written this way. Why do we write the whole code in parantheses? This concept wasn't explained in the previous videos.

1 Answer

Simon Coates
Simon Coates
8,177 Points

do you mean?

((Animal) object).makeSound()

The parenthesis are used for precedence or bundling. Usually it forces a particular order of execution. But here, (Animal) object.makeSound() would probably be illegal. It would probably attempting to call makeSound before it had been told that object was an instance of Animal (and thus that a makeSound method was accessible). A quick demo of something similar:

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

    Object o = "some string";
    //o.toUpperCase(); 
    //(String) o.toUpperCase();
    ((String) o).toUpperCase();
  }
}

In the above, only the last line is valid.

Thanks mate, it has become clearer