While learning about interface reference types I was playing around and found something odd; the code below. As you see, the line with "ibs.spaceOther(); " is confusing me.
Could someone point me in the right direction? Maybe give me a word or something for me to google?
// IBlockSignal.java
public interface IBlockSignal {
public void denySignal();
}
// SpaceTechnology.java
public class SpaceTechnology implements IBlockSignal {
@Override
public void denySignal() {
System.out.println("space-deny");
}
public void spaceOther(){
System.out.println("space-other");
}
}
// Main.java
public class Main {
public static void main(String[] args) {
IBlockSignal ibs;
ibs = new SpaceTechnology();
ibs.denySignal();
System.out.println(ibs.getClass());
// ibs.spaceOther(); // <--- Throws exception. Why? It is of class "SpaceTechnology". And this class does define spaceOther()
((SpaceTechnology) ibs).spaceOther();
}
}
////////////////////////////////////////
// Output:
//
// space-deny
// class SpaceTechnology
// space-other