Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast an object to an interface in java?

Tags:

java

interface

if we cast an object to an interface, won't this object be able to call its own methods? in the following example, myObj will only be able to call MyInterface methods?

MyInterface myObj = new Obj();

If this is correct, what is the difference between those 2 objects :

MyInterface myObj = new Obj();

MyInterface mySec = new Sec();

Thanks for your help

like image 985
Paul Avatar asked Dec 07 '11 00:12

Paul


People also ask

Can you cast an object to an interface Java?

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 cast an interface in Java?

interface I1 { } interface I2 { } class C1 implements I1 { } class C2 implements I2 { } public class Test{ public static void main(String[] args){ C1 o1 = new C1(); C2 o2 = new C2(); Integer o3 = new Integer(4); I2 x = (I2)o1; //compiler does not complain I2 y = (I2)o3; //compiler complains here !! } }

Can we cast interface to class in Java?

You can use the name of a Java interface as you would a data type when you declare a variable, a data field, or a method's parameter or return type.

Can you cast an interface to an object?

If you have a concrete class, you can cast it to the interface. 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.


1 Answers

MyInterface myObj = new Obj(); 
MyInterface mySec = new Sec(); 

For this to be legal, both Obj and Sec will have to be implementers of MyInterface. The difference between these two objects would be how they provide that implementation. Obj and Sec could do two very different or very similar things, but their commonality is that they would adhere to a contract that you could rely upon. Consider you have a method

public void doSomethingWith(MyInterface thing) {
     thing.frob();
}

Each object, myObj and mySec, could be passed into this method, and this method could then use that object's frob method (assuming frob is part of the interface declaration). This is liberating. This allows you to do very powerful things, by programming to interfaces and not to implementations. For example, you can extend functionality of classes and not change a line of code in those classes, you simply pass a different implementation of a dependency. You are not tied to, or coupled with, any one implentation inside the method doSomethingWith.

but i also read that if we declare the object myObj as MyInterface, myObj won't be able to use its own methods (from the class Obj), is that correct

Internally, instances of Obj will continue to have full access to the Obj API. myObj is still an Obj, it will always be able to use its own implementation details.

public interface MyInterface {
    void frob();
}

public class Obj implements MyInterface {

    public void frob() {
        doFrobbing();
    }

    private void doFrobbing() {
        System.out.println("frobbing");
    }

    public static void main(String[] args) {
        MyInterface myObj = new Obj();
        myObj.frob(); // still internally calls doFrobbing()
        ((Obj)myObj).doFrobbing(); // visible only via class reference
    }
}

Instances of Obj will still be instances of Obj, and those instances will still be able to use doFrobbing. Externally, persons using those instances via the interface reference will only be able to access the interface methods.

like image 120
Anthony Pegram Avatar answered Oct 21 '22 01:10

Anthony Pegram