Assignments/Article By Jakub Lisowski

Polymorphism and Inheritance in Java

Student Assignment

Polymorphism and Inheritance in Java

Published on June 12, 2022

Inheritance

It allows for one class (child class) to inherit the fields and methods of another class (parent class). For instance, we might want a child class Dog to inherit traits from a more general parent class Animal. When defining a child class in Java, we use the keyword extends to inherit from a parent class.

Inheritance provides the following benefits:

  • Code reusability: Inherited members can be used in the subclass without reimplementation.
  • Method overriding: Subclasses can provide their implementation of inherited methods, allowing for specialisation.
  • Polymorphism: Inheritance enables the use of polymorphism

Example below:

Polymorphism

It allows a child class to share the information and behaviour of its parent class while also incorporating its own functionality. This allows for the benefits of simplified syntax and reduced cognitive overload for developers.

Example below(continuation of a previous screenshot):

In Java, there are two types of polymorphism:

  • Static Polymorphism (compile-time polymorphism): This happens when a class has different methods with the same name but different parameters. The compiler decides which method to use based on the arguments provided. Examples include method overloading and constructors with different parameter lists.
  • Dynamic Polymorphism (runtime polymorphism): This occurs when a superclass reference variable is used to refer to a subclass object. The method to be executed is determined at runtime based on the actual type of the object. Dynamic polymorphism is achieved through method overriding, where a subclass provides its implementation of a method defined in the superclass.

Student Assignment

The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites

Our Latest Blog

Become a Selenium Automation Expert Ace the LSET Tester Exam & Unlock New Opportunities

Become a Selenium Automation Expert: Ace the LSET Tester Exam & Unlock New Opportunities!

Automation testing has become a key skill in today’s software industry. With businesses striving for...
Read More
Boost Your Career with the LSET Selenium Tester Exam Validate Your Automation Skills

Boost Your Career with the LSET Selenium Tester Exam

Automation testing is everywhere these days. If you’re working in software testing, you’ve probably come...
Read More

Key Takeaways from the International AI and Machine Learning Workshop with Dr. Eric Hossein Fontes

The International AI and Machine Learning Workshop, held on March 5, 2025, brought together AI...
Read More