I’ve a concrete class A that implements an interface B.
B ref = new A();
Code :
public interface B{
public abstract String[] getWords();
}
public class A implements B {
private String[] words = new String[] {};
public void setWords(String[] words){
this.words = words;
}
public String[] getWords(){
return this.words;
}
}
In the interface B, I’ve only getter method but no setter method though the class A has it.
So when I do this : B ref = new A();, will this code work and how will I set words?
You have to cast back to the original type if the interface does expose it
A better solution is to add the method to the interface.