Recommended Free Tools
Choose composition when you want to reuse, combine, or swap behavior. Choose implementation inheritance when a derived class genuinely is a subtype of its base class, callers should be able to use it as one, and the base class is designed for extension. “Favor composition over inheritance” is a useful bias for code reuse—not a rule against inheritance or polymorphism.
What inheritance and composition mean
Inheritance creates a subtype relationship
In class-based languages such as Java, a subclass inherits operations from a superclass and can override methods. Code can then use an instance of the subclass through the base type. That makes inheritance more than a way to share code: it also makes a promise about how the subclass relates to the superclass. The Oracle Java tutorial on subclasses explains Java’s class inheritance and method overriding rules.
Composition delegates to contained objects
With composition, an object holds other objects and uses their behavior. It can expose only the operations it needs, delegating each to a collaborator while keeping that collaborator’s broader API out of its own contract. Composition lets a class use a capability without claiming to be a subtype of the object providing it. These techniques can also be combined; they are not mutually exclusive. Deitel and Deitel’s textbook section on designing with composition versus inheritance treats them as alternatives and notes that a mix can be reasonable.
A practical decision sequence
-
Test whether the subtype claim is true to callers
Ask whether code that expects the base type should behave correctly when given the derived type. If the answer is yes, inheritance may fit. A phrase such as “X is a Y” is a useful prompt, not proof: the derived type must preserve the behavior callers reasonably expect from the base type.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsSpecial offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
-
Separate the contract from the implementation
If you need only a capability or a few operations, composition can provide them without exposing the entire base-class interface. A wrapper can forward selected methods and keep unrelated operations hidden. Inherit when the public subtype relationship itself is intentional, not merely because the superclass already contains convenient code.
-
Check who controls the extension points
Inheritance is safer when the superclass is specifically designed and documented for extension, or when the base and derived classes are under coordinated control. An ordinary concrete class can change in ways that break subclasses that depend on its behavior. Joshua Bloch’s Java Magazine article on inheritance and composition warns about this risk and advises favoring composition when inheritance is not appropriate.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
-
Consider what may change independently
If a collaborator or behavior may vary independently, composition usually gives it a narrower seam: the enclosing class can use a different collaborator without changing its subtype relationship. If related types form a stable domain family with shared behavior, inheritance may express that model and its polymorphic use more directly. This is a design heuristic, not a measured claim that one technique is universally faster or cheaper.
-
Choose the smallest honest public contract
Prefer the approach that exposes exactly the relationship the design intends. Use a wrapper when only selected behavior belongs in the new type’s API; use inheritance when callers should recognize and use the new type as the base type.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.Rank #3
SaleAnker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
Inheritance vs. composition at a glance
| Decision axis | Inheritance tends to fit when | Composition tends to fit when |
|---|---|---|
| Caller expectation | Callers should accept the new type anywhere the base type is expected. | The new type should expose only selected behavior. |
| Reuse goal | Shared behavior belongs to an intentional subtype hierarchy. | You want to borrow a capability or assemble behaviors. |
| Encapsulation | Extension points and superclass behavior are documented and controlled. | You want to avoid coupling to superclass implementation details. |
| Change | Base and derived types can evolve together. | Collaborators or behaviors need to change independently. |
| Variation | A stable family of related types shares a contract. | Multiple behaviors should be swappable or combined. |
These are tendencies, not guarantees. For example, a hierarchy can define a family of types while each concrete class composes strategies or services to provide variable behavior.
Common mistakes to avoid
- Inheriting just to save typing: an implementation shortcut can create a public subtype promise and couple the class to superclass behavior.
- Relying on labels alone: sharing a name or category does not establish that a derived type will preserve the base type’s expected behavior. Check substitutability.
- Applying composition mechanically: delegation adds collaborators and forwarding methods. A deliberately designed base class can express a stable polymorphic family more directly.
- Confusing class inheritance with interface implementation: the advice to favor composition concerns implementation inheritance—extending a class. Implementing an interface declares a type contract; it does not, by itself, inherit a concrete class’s implementation.
Further reading
For a Java-focused treatment, Joshua Bloch’s Effective Java, Third Edition includes Item 18, “Favor composition over inheritance.” Bloch’s Java Magazine article, adapted from that edition, puts the principle this way: “Inheritance is a powerful way to achieve code reuse, but it is not always the best tool for the job.”
Quick Recap
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

