简体   繁体   中英

Using subclass' method in superclass

I have a class (called SubClass for simplicity) that extends SuperClass and implements IClass. I know that you can call SuperClass' methods by using super.method(), but is it possible to call a method from SubClass which it implements from IClass?

Example:

public class SuperClass {

    public void method(){

        implementedMethod();

    }

}

Subclass:

public class SubClass extends SuperClass implements IClass{

    public void implementedMethod() {

        System.out.println("Hello World");

    }

}

IClass:

public interface IClass {

    public void implementedMethod();

}

I would like to call SubClass' implementedMethod() (Which it gets from IClass) from SuperClass

How would I go about doing that?

You can make the super class abstract:

public abstract class SuperClass implements IClass {
    public void method(){
        implementedMethod();
    }
}

The only way to call that method would be to create an object of type SubClass (in SuperClass) and call subClassInstance.implementedMethod().

I also want to stress that this is very inelegant. As stated in a comment on your question, you should reconsider your class designs if your superclass needs to call a subclass method.

Given the types above, anExpressionOfTypeSubClassOrIClass.implementedMethod() must be used. Note that the Type of an expression - the view it provides - must have the method intended to be used. In this case, an expression of type SuperClass cannot be used here because it has no declared implementedMethod member.

One approach - and arguably the preferred approach - is to use abstract methods . Even though abstract methods are not strictly required for Polymorphism they describe scenarios such as this where a subclass should provide the implementation. (The abstract methods could be replaced with empty method expecting - but not requiring - to be overridden in sublcasses, but why not use abstract for its designed purpose?)

abstract class SuperClass implements IClass {
    // Don't implement this, but declare it abstract
    // so that we can conform to IClass as well
    public abstract void implementedMethod();

    public void method () {
       // Now this object (which conforms to IClass) has implementedMethod
       // which will be implemented by a concrete subclass.
       implementedMethod();
    }
}

This has the "negative" aspects that SuperClass cannot be directly instantiated (it is abstract, after all) and that SuperClass must implement (or, as shown, delegate out via abstract) the expected signature. In this case I also chose to make SuperClass implement IClass even though it's not strictly required because it guarantees that the SuperClass and all subclasses can be viewed as an IClass.

Alternatively, remember that Types of Expressions are just views of objects and are not necessarily the same as the actual Concrete Type of object. While I would advise against using the following code because it loses some type-safety , I think it shows the important point.

class SuperClass {
    public void method () {
        // We try to cast and NARROW the type to a
        // specific "view". This can fail which is one
        // reason why it's not usually appropriate.
        ((IClass)this).implementedMethod();
    }
}

class SubClass extends SuperClass implements IClass {
  // ..
}

class BrokenSubClass extends SuperClass () {
}

// OK! Although it is the SAME OBJECT, the SuperClass
// method can "view" the current instance (this) as an IClass
// because SubClass implements IClass. This view must be
// explicitly request through a cast because SuperClass itself
// does not implement IClass or have a suitable method to override.
(new SubClass()).method();

// BAD! ClassCastException, BrokenSubClass cannot be "viewed" as IClass!
// But we didn't know until runtime due to lost type-safety.
(new BrokenSubClass()).method();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM