{"id":1189,"date":"2012-01-08T12:55:53","date_gmt":"2012-01-08T10:55:53","guid":{"rendered":"http:\/\/www.tutego.de\/blog\/javainsel\/?p=1189"},"modified":"2012-01-08T12:55:53","modified_gmt":"2012-01-08T10:55:53","slug":"inselupdate-vererbung-und-berschattung-von-statischen-variablen","status":"publish","type":"post","link":"https:\/\/www.tutego.de\/blog\/javainsel\/2012\/01\/inselupdate-vererbung-und-berschattung-von-statischen-variablen\/","title":{"rendered":"Inselupdate: Vererbung und &Uuml;berschattung von statischen Variablen"},"content":{"rendered":"<p>Die Konstanten einer Schnittstelle k\u00f6nnen einer anderen Schnittstelle vererbt werden. Dabei gibt es einige kleine Einschr\u00e4nkungen. Wir wollen an einem Beispiel sehen, wie sich die Vererbung auswirkt, wenn gleiche Bezeichner in den Unterschnittstellen erneut verwendet werden. Als Basis unseres Beispiels steht eine Schnittstelle BaseColors mit ein paar Deklarationen von Farben. Zwei Unterschnittstellen erweiterten BaseColor, einmal CarColors und PlaneColors, die f\u00fcr Farbdeklarationen f\u00fcr Autos und Flugzeuge stehen. Eine besondere Schnittstelle FlyingCarColors erweitert die beiden Schnittstelen CarColors und PlaneColors, denn es gibt auch fliegende Autos, die eine Farbe haben k\u00f6nnen. <\/p>\n<pre><pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><span style=\"color: #0000ff\">interface<\/span> BaseColors\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">{\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">int<\/span> WHITE   = 0;\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">int<\/span> BLACK   = 1;\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">int<\/span> GREY    = 2;\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">}\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><span style=\"color: #0000ff\">interface<\/span> CarColors <span style=\"color: #0000ff\">extends<\/span> BaseColors\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">{\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">int<\/span> WHITE   = 1;\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">int<\/span> BLACK   = 0;\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">}\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><span style=\"color: #0000ff\">interface<\/span> PlaneColors <span style=\"color: #0000ff\">extends<\/span> BaseColors\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">{\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">int<\/span> WHITE   = 0;\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">int<\/span> GREY    = 2;\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">}\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><span style=\"color: #0000ff\">interface<\/span> FlyingCarColors <span style=\"color: #0000ff\">extends<\/span> CarColors, PlaneColors\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">{\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">}\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><span style=\"color: #0000ff\">public<\/span> <span style=\"color: #0000ff\">class<\/span> Colors\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">{\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">public<\/span> <span style=\"color: #0000ff\">static<\/span> <span style=\"color: #0000ff\">void<\/span> main( String[] args )\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  {\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">    System.out.println( BaseColors.GREY );      <span style=\"color: #008000\">\/\/ 2<\/span>\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">    System.out.println( CarColors.GREY );       <span style=\"color: #008000\">\/\/ 2<\/span>\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">    System.out.println( BaseColors.BLACK );     <span style=\"color: #008000\">\/\/ 1<\/span>\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">    System.out.println( CarColors.BLACK );      <span style=\"color: #008000\">\/\/ 0<\/span>\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">    System.out.println( PlaneColors.BLACK );    <span style=\"color: #008000\">\/\/ 1<\/span>\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">    System.out.println( FlyingCarColors.WHITE );<span style=\"color: #008000\">\/\/ \uf04e The field FlyingCarColors.WHITE is ambiguous<\/span>\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">    System.out.println( FlyingCarColors.GREY ); <span style=\"color: #008000\">\/\/ \uf04e The field FlyingCarColors.GREY is ambiguous<\/span>\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  }\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">}<\/pre>\n<p>Die erste wichtige Tatsache ist, dass unsere drei Schnittstellen ohne Fehler \u00fcbersetzt werden k\u00f6nnen, aber nicht die Klasse Colors. Das Programm und der Compiler zeigen folgendes Verhalten:<\/p>\n<ul>\n<li>Schnittstellen vererben ihre Eigenschaften an die Unterschnittstellen. CarColors und auch PlaneColors erbten die Farbe WHITE, BLACK und GREY aus BaseColors.<\/li>\n<li>Konstanten d\u00fcrfen \u00fcberschattet werden. CarColors vertauscht die Farbdeklarationen von WHITE und BLACK und gibt ihnen neue Werte. Wird jetzt der Wert CarColors.BLACK verlangt, liefert die Umgebung den Wert 0, w\u00e4hrend CarColors.BLACK 1 ergibt. Auch PlaneColor \u00fcberdeckt die Konstanten WHITE und GREY obwohl sie Farbe mit dem gleichen Wert belegt sind.<\/li>\n<li>Erbt eine Schnittstelle von mehreren Oberschnittstellen, so ist es zul\u00e4ssig, wenn die Oberschnittstellen jeweils ein gleichlautendes Attribut haben. So erbt etwa FlyingCarColors von CarColors und PlaneColors den Eintrag WHITE, BLACK und GREY.<\/li>\n<li>Unterschnittstellen k\u00f6nnen aus zwei Oberschnittstellen die Attribute gleichen Namens \u00fcbernehmen, auch wenn sie einen unterschiedlichen Wert haben. Das testet der Compiler nicht. FlyingCarColors bekommt aus CarColors ein WHITE mit 1 aber aus PlaneColors das Wei\u00df mit 0. Daher ist auch der Zugriff FlyingCarColors.WHITE in dem Beispiel Colors auch nicht m\u00f6glich und f\u00fchrt zu einem Compilerfehler. Bei der Benutzung muss ein unmissverst\u00e4ndlicher qualifizierter Name verwendet werden, der deutlich macht, welches Attribut gemeint ist, also zum Beispiel CarColors.WHITE oder PlaneColors.WHITE. \u00c4hnliches gilt f\u00fcr die Farbe GREY. Obwohl Grau durch die urspr\u00fcngliche Deklaration bei BaseColor und auch bei der \u00dcberschattung in PlaneColors immer Zwei ist, ist die Nutzung durch FlyingCarColors.GREY nicht zul\u00e4ssig. Das ist ein guter Schutz gegen Fehler, denn wenn der Compiler dies durchlie\u00dfe, k\u00f6nnte sich im Nachhinein die Belegung von GREY in BaseColors oder PlaneColors ohne Neu\u00fcbersetzung aller Klassen \u00e4ndern, und zu Schwierigkeiten f\u00fchren. Diesen Fehler \u2013 die Oberschnittstellen haben f\u00fcr eine Konstante unterschiedliche Werte \u2013 m\u00fcsste die Laufzeitumgebung erkennen. Doch das ist nicht m\u00f6glich und in der Regel setzt der Compiler die Werte auch direkt in die Aufrufstelle ein und ein Zugriff auf die Konstantenwerte der Schnittstelle findet nicht mehr statt.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Die Konstanten einer Schnittstelle k\u00f6nnen einer anderen Schnittstelle vererbt werden. Dabei gibt es einige kleine Einschr\u00e4nkungen. Wir wollen an einem Beispiel sehen, wie sich die Vererbung auswirkt, wenn gleiche Bezeichner in den Unterschnittstellen erneut verwendet werden. Als Basis unseres Beispiels steht eine Schnittstelle BaseColors mit ein paar Deklarationen von Farben. Zwei Unterschnittstellen erweiterten BaseColor, einmal [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[11],"tags":[],"class_list":["post-1189","post","type-post","status-publish","format-standard","hentry","category-insel"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/comments?post=1189"}],"version-history":[{"count":1,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1189\/revisions"}],"predecessor-version":[{"id":1190,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1189\/revisions\/1190"}],"wp:attachment":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/media?parent=1189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/categories?post=1189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/tags?post=1189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}