Sidz
Sidz

Reputation: 183

Why are parentheses used around a class name in Java?

I came across some code and cannot understand a certain aspect of it although i have done some extensive searching!

My question is: why are classes sometimes declared within parentheses like in the following code?

public class Animal {

    public static void hide() {
        System.out.println("The hide method in Animal.");
    }

    public void override() {
        System.out.println("The override method in Animal.");
    }
}

public class Cat extends Animal {

    public static void hide() {
        System.out.println("The hide method in Cat.");
    }

    public void override() {
        System.out.println("The override method in Cat.");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = (Animal)myCat;
        myAnimal.hide();
        myAnimal.override();
    }
}

where my focus is on this line of code in particular:

            Animal myAnimal = (Animal)myCat;

I believe it has something to do with the fact that one class extends another but am unsure what the class defined within parentheses signifies.

Any help on this is appreciated. Thank you in advance.

Upvotes: 15

Views: 14869

Answers (4)

Nathan Hughes
Nathan Hughes

Reputation: 96454

What you're seeing is a cast to tell the compiler it's ok to assign a Cat to an Animal.

Casts are for cases where the compiler can't safely convert one type to another, either because it doesn't know what the types involved are, or because the conversion would lose information (for instance, if you have a double that you want to store in a variable of type float).

In your example the cast is totally unnecessary because Cat extends Animal; since all Cats are Animals, the compiler doesn't need an explicit cast to tell it it can assign a Cat to an Animal.

Casting should be reserved for special occasions. If you use explicit casts regularly then it may mean you're not getting the most benefit out of the type system.

Upvotes: 16

Shilpa
Shilpa

Reputation: 106

Even though the code produces correct output, if you have to cast the object to a parent class and then call the static method of the parent class, the classes may need to be designed in better fashion. Inheritance in Java is supposed to provide access to common methods and fields. If one needs to code a static method in a Parent class and call it using object of child classes, then the relationship between parent and child class is not clear cut.

Upvotes: 1

Oded
Oded

Reputation: 499362

This is a cast - the myCat variable which is of type Cat is being cast to the base class Animal.

This means it can be considered to be an Animal.

This is a basic function of inheritance in Java.


As @Sebastian Koppehel commented, the cast in this case is not needed - the myAnimal variable (of type Animal) can accept any type that implements Animal.

Upvotes: 7

kosa
kosa

Reputation: 66667

You are explicitly guaranteeing compiler/runtime that the cat reference type you are passing is type of Animal. In java, it is called as casting.

While runtime if this contract is not honored (That means there is no relationship defined between cat and Animal) you will get ClassCastException.

Upvotes: 0

Related Questions