0

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

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

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,

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert