Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are parentheses used around a class name in Java?

Tags:

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.

like image 419
Sidz Avatar asked Jul 30 '12 19:07

Sidz


People also ask

Why do we use parentheses in Java?

{} are used to define a dictionary in a "list" called a literal parentheses in java: Parentheses are used for two purposes: (1) to control the order of operations in an expression, and (2) to supply parameters to a constructor or method. ................. and many more in simple: A parenthesis is a punctuation mark ...

What does class [] mean in Java?

What Does Class Mean? A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

What goes in the parentheses of a method in Java?

They are called parameters. That is how you give data to the function. You can have a method receive multiple parameters with that syntax, or none at all depending on what it does.

What goes in the parentheses after a method name?

2 Answers. Show activity on this post. Parentheses after a name means a function/method is called there. The functions or object methods are called similarly using parentheses.


2 Answers

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.

like image 95
Nathan Hughes Avatar answered Sep 21 '22 14:09

Nathan Hughes


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.

like image 23
Oded Avatar answered Sep 20 '22 14:09

Oded