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.

  • And how do you imagine it and for what? - smackmychi
  • The question is not, why do it, the question is how to do it. )) - ulcigor
  • @ulcigor:> assign a class B to class A It's like, sorry? What should be the apparent effect of this? - VladD
  • 2
    @ulcigor once again read and try to understand. You have two figures. The shape is their common interface. There are two implementations circle and square. So here you are in the code and it turns out. Interface Figure {...}; The Circle class implements the interface. Figure {...}; class Square implements interface Figure {...}; Square square = (Square) a new object of the Circle class; // PROFIT ... Do not confuse anything? Hear yourself at least once - everything will fall into place. - smackmychi
  • 2
    @ulcigor, why do you need this? If you want to convert class A to class B, then create a separate method that will do this. Yes, and in your example the class CWebsocket does not implement the Session interface. - skubarenko

1 answer 1

Minute verbiage and abstract reasoning.

The concept of an interface in OOP does not imply that the two classes that implement it should be interchangeable. The meaning of the interface is that the classes that implement it can perform the same actions . That is, this does not mean that one such class can be replaced by another. This means that the behavior of one class can be replaced by the behavior of another. It seems to be a small difference, but in fact it is significant. Let's say a person can walk (implements the corresponding interface), but the Clock can also walk (they also implement the interface). That is, the Man and the Clock can perform the same action (albeit in completely different ways), but it cannot be said that the object of the Man class can be replaced with an instance of the Clock class - there is nothing in common between them, except that they can perform one action with two different in ways. That is, your assumption that two different implementations of the same interface can be "assigned" to each other is initially wrong.

  • What a thick demagoguery ("walk"). Walk - function and walk - move refer to different "interfaces". Let's also drag the fishermen here, who go to the sea like a clock. - smackmychi
  • "thick demagoguery" was dragged in as a matter of fact not accidentally, namely, in order to demonstrate that the two implementations of one interface do not have to be (and are not) interchangeable. It is this very lack of commonality between the two entities that is intended to emphasize the idea that you should not try to push two interface implementations under a common denominator - the goal of interfaces is to provide similar behavior, and not to ensure the interchangeability of entities - DreamChild
  • It's funny, because the author does not understand direct speech, it is not that demagoguery. - smackmychi
  • pardon me, why are you so angry about the author-question? Straight as if he offended you with something - DreamChild
  • @DreamChild I just stated the fact. - smackmychi