简体   繁体   中英

Referencing through super class/interface reference - Java

I am new to Java and I understand the underlying basic concepts of inheritance. I have a question regarding referencing through superclass. As the methods of class inherited from a superclass or implemented using an interface can be referenced through a superclass reference (interface or class). How would it work when both extends and implements are involved with a class?

class A {
  void test() {
    System.out.println("One");
  }
}

interface J {
  void first();
}

// This class object can referenced using A like A a = new B()
class B extends A {
  // code    
}

// This class object can referenced using J like J j = new B()
class B implements J {
  // code
}

// my question is what happens in case of below which referencing for runtime polymorphism?
class B extends A implements J {
  // code 
}

Which fails to compile with:

Main.java:16: error: duplicate class: B
class B implements J {
^
Main.java:21: error: duplicate class: B
class B extends A implements J {
^
2 errors

How would it work when both extends and implements are involved with a class ?

Assuming that is your question.

The extends keyword is for extending a superclass.

The implements is for implementing an interface


The difference between an interface and a superclass is that in an interface you cannot specify an specific implementation of the whole (only its "interface"- Interfaces cannot be instantiated, but rather are implemented ).So this means you can only specify methods you want to incorperate, but not implement them on your project in the same sense.

There can be some differences when referencing super class method vs interface methods, notably when you use super to call them. Consider these interfaces/classes:

public interface MyIFace {
    void ifaceMethod1();
}


public class MyParentClass {
    void parentClassMethod1();
}

public class MyClass extends MyParentClass implements MyIFace {

    public void someChildMethod() {
        ifaceMethods(); // call the interface method
        parentClassMethod1(); // call the parent method just like you would another method. If you override it in here, this will call the overridden method
        super.parentClassMethod1(); // you can use for a parent class method. this will call the parent's version even if you override it in here
    }

    @Override
    public void ifaceMethod1() {
      // implementation
    }

}

public class AnExternalClass {
    MyParentClass c = new MyClass();
    c.parentClassMethod1(); // if MyClass overrides parentClassMethod1, this will call the MyClass version of the method since it uses the runtime type, not the static compile time type
}

In general, calling the method without super will call the method implemented by runtime version of the class (regardless of whether the method is from a class or interface)

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