Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit class without parent type in Java

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?

like image 588
Daniel Avatar asked Feb 18 '26 00:02

Daniel


1 Answers

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.

like image 197
Micha Wiedenmann Avatar answered Feb 20 '26 14:02

Micha Wiedenmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!