Inherit class without parent type in Java

  • A+
Category:Languages

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?

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: