Inheriting a class without a parent type in Java

How do I inherit from other classes without using a parent type?

For example, I hope that car can adopt all methods and attributes of vehicle without being recognized as object of vehicle type

Is there a convenient way to do this in Java without repeating all methods and properties?

Solution

It depends on your convenient definition You are looking for forwarding / composition / aggregation, basically:

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

This means that all methods defined in a need to be repeated in B

This often occurs in the discussion of inheritance vs. aggregation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>