Good evening, dear forum users.
There was a question of inheritance.
There is an interface and two implementations of this interface. Suppose classes A and B. When initializing class A and class B, you must assign class A to class B. The question is, in fact, how to do this? When you try to do this, it is quite expected that java throws an Exeption java.lang.ClassCastException with a description that cannot be cast to .
Here is a sample code:
public interface Session { public String getName(); public void setName(String name); } public class Websocket implements Session { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public class CWebsocket implements Session { private Integer id; public int getId() { return id; } public void setId(int id) { this.id = id; } } public class Test { public static void main(String [] args) { Websocket websocket = new Websocket(); websocket.setName("test"); CWebsocket cWebsocket = (CWebsocket) websocket; System.out.println(cWebsocket.getName()); } } Thank you in advance for your attention to the problem.