Part of me thinks that this should not be possible (even if it is), but I will ask anyway.
Given the following class hierarchy ( Grandparentand Parentbelong to a third party and therefore not under my control), how would I override myMethod()in Childso that it bypasses the overridden implementation in Parentand calls the one that is in Grandparent?
Grandparent
Parent
myMethod()
Child
class Grandparent { public void myMethod() { // do stuff } } class Parent extends Grandparent { @Override public void myMethod() { super.myMethod(); // do something else } } class Child extends Parent { @Override public void myMethod() { // ??? I want to *only* do what Grandparent did here } }
Imagine that a class Parentprovides many other useful actions and is a key element of the hierarchy Child(in other words, I'm not looking for a “make Childsubclass Grandparent”.
, , , - .
, , .
, , , , , "Child" "Parent" "Grandparent".
, : " - , ?" , Child, , Parent?
, : -, (.. Ford IS ALO a Car, "" "" ).
, Parent Grandparent , , (, -, ) :
"" ( , , ) myMethod(). , , myMethod() / , "" "".
, , , , , . / myMethod() , . , .
Seb, , . . - .
.
final . ( ) .
final
class Grandparent { public final void myHelperMethod() { // do stuff } public void myMethod() { myHelperMethod(); } } class Parent extends Grandparent { @Override public void myMethod() { super.myMethod(); // do something else } } class Child extends Parent { @Override public void myMethod() { // ??? I want to *only* do what Grandparent did here myHelperMethod(); } }
?
, (myNewMethod) , myMethod , myNewMethod Child?
( Java, , )
class Grandparent { public void myMethod() { myHelperMethod(); } } class Parent extends Grandparent { @Override public void myMethod() { super.myMethod(); // do something else } public final void myNewMethod() { super.myMethod(); } } class Child extends Parent { @Override public void myMethod() { // ??? I want to *only* do what Grandparent did here myNewMethod(); } }
Source: https://habr.com/ru/post/1706056/More articles:pasting a value into an SQL table and using a SELECT statement - sqlWhat common backend can I safely get from the iPhone and Android app? - androidHow to specify an order to the delivery address and billing address in rails - ruby-on-railsRails Частичная рендеринг приводит к пустой странице? - ruby-on-railsRhino Mocks: метод mocked возвращает null - mockingAltGr in System.Windows.Forms.Keys enumerator - c #Fake forms with C # WebClient - c #How to get NHibernate SchemaExport to create SQL Server Timeramp columns? - c #How to freeze a rake? - ruby | fooobar.comHow to bind a UserControl property? - silverlightAll Articles