Syntaktische Kompatibilität

thomashuuu

Mitglied
Hallo,

ich habe eine kurze Verständnisfrage, vielleicht kann mir ja jemand helfen.

Wenn A ein Subtyp von B ist (also kovarianter Rückgabetyp und kontravarianter Argumenttyp), ist A dann syntaktisch kompatibel zu B oder ist es umgekehrt und B ist syntaktisch kompatibel zu A?
Also: A Subtyp von B = A kompatibel zu B
oder A Subtyp von B = B kompatibel zu A ?

Also wenn die Frage ist: „sind die Operationen des spezielleren Interface (Sub) kompatibel zu denen des allgemeineren Interface(Super)?“, dann teste ich mit Kontravarianz und Kovarianz ob das spezielle Interface ein Subtyp von dem anderen ist und wenn das der Fall ist, kann ich die Frage mit JA beantworten?

Vielen Dank schonmal!
 

ceving

Aktives Mitglied
Wenn A ein Subtyp von B ist (also kovarianter Rückgabetyp und kontravarianter Argumenttyp), ist A dann syntaktisch kompatibel zu B oder ist es umgekehrt und B ist syntaktisch kompatibel zu A?
Also: A Subtyp von B = A kompatibel zu B
oder A Subtyp von B = B kompatibel zu A ?

Definiere "syntaktisch kompatibel"? Zuweisungskompatibel?
 

Gucky

Top Contributor
Wenn du das meinst, was mein Vorredner vermutet, also A extends B dann kannst du einer Variable vom Typ B eine Instanz vom Typ A zuweisen.
 

thomashuuu

Mitglied
ja, genau ich meine zuweisungskompatibel. Also kann ich sagen : A extends B = A kompatibel zu B, oder eben andersrum A extends B = B kompatibel zu A? Mein denkfahler liegt denke ich in dieser Formulierung...
 

turtle

Top Contributor
@Gucky:
Habe ich class A und class B exends A, dann kann man nicht einer Variablen vom Typ B eine Instanz vom Typ A zuweisen.

Java:
	B b = new A(); // Type mismatch: cannot convert from A to B
	A a = new B(); // klar geht das, denn jedes B IST-EIN A
 

Gucky

Top Contributor
Ich hatte die Buchstaben auch andersrum.
A extends B
JFrame extends JComponent

B varb = new A();
JComponent varjc = new JFrame();
 

Neue Themen


Oben