Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Verständnisfrage zu Vererbung

Empfohlene Antworten

Veröffentlicht

Habe folgende Frage zu:

public abstract class High

{

public High(int a) {

this.a = a;

System.out.println("High : Konstruktor " + a);

}

public void function1() {

System.out.println("High.function1() " + a*2);

}

public abstract void view();

protected int a;

}

public class Medium extends High

{

public Medium(int n)

{

super(n/2);

System.out.println("Medium: Konstruktor " + a);

this.b = n;

}

public void view() {

System.out.println("Medium.view(), a=" + a + ", b=" + B);

}

protected double b;

}

public class Low extends Medium

{

public Low(int n)

{

super(n/2);

System.out.println("Low : Konstruktor " + n);

this.c = n;

}

public void function1() {

System.out.println("Low.function1() " + (a * B));

}

public void function2() {

System.out.println("Low.function2() " + (a + B));

}

private int c;

}

Wenn ich folgende Testklasse habe und folgende Methode. Wieso geht folgendes nicht:

public void a() {

Low l1 = new Low(9);

l1.function1();

l1.view();

High h1 = l1;

h1.function1();

h1.function2(); **

}

Wieso compielt der Compiler diese Zeile nicht und meldet mir den Fehler "cannot resolve symbol - method function2()"! **

Das ist mir absolut klar, wenn er doch h1.function1(); ausführt, muss das andere doch auch gehen.

Danke!

Ich versteh dein coding nicht wirklich, aber in der High klasse ist keine Funktion 2 drin... kanns sein das es desswegen nicht geht? :confused:

Du hast ihm gesagt das h1 ein High sei:

High h1 = l1; 

Beim Aufruf von der Funktion 2 denkt er es immer noch, obwohl du ja l1 zugewiesen hast.

Du musst ihm jetzt nur sagen, dass er ein Low sein. Das ganze heißt Type-Cast:

(Low)h1.function2(); 

Mfg MAtczek

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.