简体   繁体   中英

Java and property overload

I'm stucked on this issue using eclipse 2018-12 (4.10) / Java 11 on debian 9

Let's state I've a base class

class A {

   private typeA prop;

   TypeA getProp(){
      return this.prop;
   }
}

and a derived one

class B extends A{
 private typeB prop;

 TypeB getProp(){
    return this.prop;
}

I'm expecting that prop in class B hides the prop in class A, but in eclipse I've got an error Message in class B for the getProp method : The return type is incompatible with A.getProp()

any idea ?

通过TypeA继承-> TypeB使用JB Nizet注释解决

you can try using interfaces like this:

interface Type {
    // ...
}

class TypeA implements Type {
    // ...
}

class TypeB implements Type {
    // ...
} 

class A {

   private TypeA prop;

   Type getProp(){
      return this.prop;
   }
}

class B extends A {

    private TypeB prop;

    Type getProp(){
        return this.prop;
    }
}

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