简体   繁体   中英

Identifying the relationship between interface and inheritance of different classes in Java

I am trying to identify all error in coding below. There are 4 classes,

  • A: Interface
  • AImpl: Class implementing A
  • B: Parent Class of C
  • C: Child Class of B

     public interface A { public void doSomeProcess(String s); } public class AImpl implements A { public void doSomeProcess(String s) { System.out.println("A Imple Code"); [. . .] } } public class B { public A doIt() { [. . .] } public String execute() { [. . .] } } public class C extends B { public AImpl doIt() { [. . .] } public Object execute() { [. . .] } } 

Here's my understanding,

  1. A and AImpl are fine since A is an interface class and AImpl has to implement abstract method in A to fulfill the contract.
  2. Class C should be extends from Class B public class B extends C
  3. execute() in Class C is overriding method

Problem is how should I deal with public A doIt() and public AImpl doIt() ? Can an interface class be a return value?

Thanks!

There is no term 'interface class' in Java. It should be either a interface or a class.

Yes, interface could be a return type. And the benefit of polymorphism is seen in such a use-case.

Yes we can have a return type with interface type

For ex:

interface Bounceable
{ 
    Bounceable isBouncing();
}

Class Tyre implements Bounceable
{

  @Override
  Bounceable isBouncing()
  {
     Bounceable myVariable;

     //your code 

     return myVariable;
  }

}

Problem is how should I deal with public A doIt() and public AImpl doIt()? Can an interface class be a return value?

Without any problem YES , from 1.5 version co-variant return types are allowed in overriding, ie if a child class is overriding a parent method, then method return type need not be of same type as parent method return type, it can be of it's child type as well. In your case doIt() of child method can return either of type A or it's child AImpl

One major aspect of interfaces is to use them all over the place. Simply because the caller of some method doesn't need to know about the specific implementation class.

Assume that your method does return List . Most of the time, that is perfectly fine. No caller needs to worry whether the method returns an ArrayList or a LinkedList or Whatever List!

So returning A is perfectly fine, it should rather be preferred to be used!

Yes interface type you can use it as return type (It can hold reference to object of any class which directly or indirectly implements that interface)

In your case it's covariant return type when you are overriding doIt() method in class C. (As overrided doIt() method's return type in class C can get upcast to return type of doIt() method in class B. it's allowed) AImpl can get upcast to A because AImpl is child of A.

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