#Interface Inheritance

1 messages · Page 1 of 1 (latest)

weary radish
#

Why can't I call previous implementations of the show() method in this example?

public interface TopLevel {
    default void show() {
        System.out.println("Top level");
    }
}
public interface MidLevel extends TopLevel {
    @Override
    default void show() {
        System.out.println("Mid Level");
    }
}
public interface LowLevel extends MidLevel {
    @Override
    default void show() {
        System.out.println("Low Level");
    }
}
public class Lmao implements LowLevel{
    @Override
    public void show() {
        System.out.println("Lmao");
        LowLevel.super.show();
        MidLevel.super.show(); //Error
    }
}
sacred sedgeBOT
#

<@&987246399047479336> please have a look, thanks.

remote sundial
#

Well, if you call super.show(), you will call LowLevel.show(). But LowLever overrides the MidLever show.

#

Your class lmao overrides the LowLevel show()

weary radish
#

Yeah, but why I can call LowLevel.super.show() but can't MidLevel or TopLevel

sour goblet
#

LowLevel.... in this context is identical to not writing it at all

#

so what u wrote is just super.show()

#

u can't put the class name there to access a specific version of the method, that's not a thing

#

the only reason u could write LowLevel there is bc ur current class is LowLevel

#

its more or less just ignored

#

the class name in front makes sense for static methods

#

but u dont have a static method here

peak urchin
#

in my computer, default void show() { isnt useble at all. how could it be

#

?

weary radish
peak urchin
#

but what does the code have to do in general?

weary radish
#

It's just test of InterfaceName.super.defaultMethod() construction in general

sour goblet
#

what u wrote makes, java-wise, no sense. and hence doesnt work

#

and LowLevel.super.show() is simply identical to just writing super.show()

peak urchin
weary radish
#

I tried to use InterfaceName.super.defaultMethod() construction with inheritance and I was stumped

#

Book is java - the complete reference book Herbert Schildt

#

If I write MidLevel.super.show() in the LowLevel interface instead of in the Lmao class it works, but I don't understand why

peak urchin
#

im a bit new to java, but if im not wromg, ur book may be up to date

#

from what year is it?

weary radish
#

This edition about java 17 version

#

I'm using java 21

#

But I don't think it should create conflict

#

Backward compatibility is one of the main principles of java, after all

peak urchin
weary radish
#

I have an idea that in this way you can call an implementation only 1 level higher in the inheritance chain, but I haven't found any confirmation of this, so I can't be 100% sure about it

#

Because

public interface LowLevel extends MidLevel {
    @Override
    default void show() {
        //MidLevel.super.show();      NO ERROR
        System.out.println("Low Level");
    }
}
``` gonna work
sour goblet
# weary radish

whats written in that book is correct but ur code doesnt represent that situation

sacred sedgeBOT
sour goblet
#

u cant refer to ur grandfather, as that is the business of ur direct father to deal with

#

thats how inheritance works

sour goblet
#

yes. u can either say "my stuff or the stuff of my father"

#

u cant say "or my grandfathers"

weary radish
#

Oh, thank you very much

sour goblet
#

what the book shows is sth different. its for when u have two direct parents

#

i.e. the diamond problem

weary radish
#

I understand it now

peak urchin
#

oh now its clear.thnx

sacred sedgeBOT
#

@weary radish

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍