When can an object reference be cast to an interface reference?

When can an object reference be cast to an interface reference?

Yes, you can. If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.

How do you reference an interface in Java?

You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface. By casting object1 to a Relatable type, it can invoke the isLargerThan method.

What is the use of interface reference variable in Java?

A reference variable declared using an interface name as its type can only reference instances of classes that implement that interface. For example, Java provides an interface called Runnable. Java also provides a class called Thread that implements Runnable.

Can we create a reference to an interface?

We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it that refers to the Object of its implementing class. A class can implement more than one interface. A class that implements interface must implements all the methods in interface.

Why should I use interface?

Why do we use interface ? It is used to achieve total abstraction. Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance . The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static.

Can we make reference of interface?

We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it that refers to the Object of its implementing class. An interface can extends another interface or interfaces (more than one interface) . A class that implements interface must implements all the methods in interface.

What is the use of an interface reference give example?

Can we cast an object reference to an interface reference in Java?

To make this program work you need to remove the line calling the method of the class as − Now, the program gets compiled and executed successfully. Therefore, you need to cast an object reference to an interface reference. Whenever you need to call the methods of the interface only.

Can a class be a reference to an interface?

If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.

Can you cast interface to concrete implementation object?

If you have an interface, it is possible to cast to the concrete class. Generally, you only want to go in the first direction. The reason being that you shouldn’t know what the concrete class is when you have only a pointer to the interface.

How to call the methods using interface reference?

In order to call the methods using interface reference (here r is interface reference), you have to assign to class object to it. Like if you are assigning Person1’s object obj1 to r i.e. r = obj1; then you call the Speed () and Distance () methods that are implemented by the Person1 class.