Inheritance and Polymorphism in Programming

Need help with assignments?

Our qualified writers can create original, plagiarism-free papers in any format you choose (APA, MLA, Harvard, Chicago, etc.)

Order from us for quality, customized work in due time of your choice.

Click Here To Order Now

Inheritance is one of the core concepts of object-oriented programming. It allows classes to receive, or inherit, all the behaviors and attributes of other classes (Farrell, 2016). Classes that inherit from others can still have behaviors and attributes unique to them (Farrell, 2016). Therefore, inheritance is a useful technique when creating objects that are sufficiently similar to share some behaviors, but not sufficiently similar to allow them to be defined by one class. This allows code to be reused throughout the program and helps save time and avoid errors that can occur when creating similar code for similar classes. Examples of such use can be in a graphical application: geometric shapes can be defined by an array of points and share a method that draws them on the screen. Each subclass (rectangle, triangle, polygon) would need a unique method to determine its area.

Polymorphism is another key concept of object-oriented programming; it allows the program to interact with objects as instances of multiple classes. This allows one to interact with similar objects in different ways depending on the context and specific requirements of the interaction. For instance, an application that works with employee records can implement subclasses for different specific positions with unique behaviors. However, certain behaviors can be shared, for instance, sending an email or altering ones schedule is independent of ones position. Furthermore, under polymorphism, a subclass can override the methods of its superclass, providing unique behaviors. Going back to the graphical application example, a Polygon superclass can provide a GetArea() method suitable for most shapes. However, specific shapes can override it if a more efficient formula exists to determine their area. In such a situation, polymorphism provides additional means of reusing existing code, saving time and preventing errors, supplementing inheritance.

Reference

Farrell, J. (2016). Java Programming (8th ed.). Boston, MA: Cengage Learning.

Need help with assignments?

Our qualified writers can create original, plagiarism-free papers in any format you choose (APA, MLA, Harvard, Chicago, etc.)

Order from us for quality, customized work in due time of your choice.

Click Here To Order Now