Inherit class without parent type in Java

Daniel :

How can I inherit from another class without adopting the parent type?

For example I want to be able to let Car adopt all methods and properties of Vehicle without being recognized as an Object of type Vehicle.

Is there a convenient way of doing this in Java without duplicating all methods and properties?

Micha Wiedenmann :

It depends on your definition of convenient. You are a looking for Forwarding/Composition/Aggregation, essentially:

class B {
  A a;
  T foo() { return a.foo(); }
}

Which implies that all methods defined in A need to be duplicated in B.

This often comes up in discussion about Inheritance vs. Aggregation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=84999&siteId=1