When can object reference cast to interface reference

When can object reference cast to interface reference

Answer: When java class implements interface it can be cast into interface reference. Please see example below:

Interface ABC.java

package com.javahonk.interfacereference;

public interface ABC {

    void testMethod();

}

 

Class which implements interface ABC:

package com.javahonk.interfacereference;

public class InterfaceObjectReference implements ABC {

    public static void main(String[] args) {
	ABC abc = new InterfaceObjectReference();
	abc.testMethod();

    }

    @Override
    public void testMethod() {
	System.out.println("Calling through "
		+ "interface ojbect reference");

    }

}

 

Output:

When can object reference cast to interface reference

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *