Advanced Java/Reuse
Subject classification: this is an information technology resource. |
The term reuse describes one of the four pillars of object oriented software systems academically referred to as Inheritence.
In Java, objects can be declared as an extension of another object. While there may be new or changed methods, Java will also inherit the existing methods of the original object.
Reuse example
[edit | edit source]The most common example of reuse is used in various Graphical User Interfaces. The most basic example would be a checkbox used to toggle something on or off. A radio button can be a subclass of this checkbox and reuse existing code, but is also given the ability to automatically uncheck similar radio buttons within a given group.
Reuse limitations
[edit | edit source]An example that will cause problems with reuse is the difference between a Square and Rectangle. While you could in theory try to declare a Square as a subclass of Rectangle (or vice versa), they are defined differently. A square is a figure with four equal sides, while a rectangle has both opposite sides the same length. Attempting to declare a square as a subclass of rectangle will cause problems if you try resizing the shape as if it were a Rectangle (preventing reuse in that instance), and you cannot subclass a rectangle under a square since not all rectangles are squares.
As such, rules of inheritance may not function as expected even if it would appear to make sense in mathematics.