Software Development

Inheritance and Delegation Design Pattern Examples in Java


Udemy

This post shows 2 sets of rather equivalent Java source codes. One uses inheritance that enable child classes to inherit properties and methods from their parent classes. Another set uses the Delegation Design Pattern.

Inheritance

[wp_ad_camp_1]

With inheritance, we use the extends keyword in Java. The PitBull class will have the name property and getter/setter methods PLUS strayOrNot and its associatd getter/setter methods.

Delegation Pattern

[wp_ad_camp_2]

Put simply, the class which other classes supposedly inherits their properties and methods from is made a reference variable in those “child” classes. Method calls are routed accordingly to call methods on an object of that reference variable type.

For example,

[wp_ad_camp_3]

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like