Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Polymorphism

Lesson 23 of 39 in the free Java notes on Siksha Sarovar, written by Rohit Jangra.

Polymorphism

(poly = many, morph = form). It is the ability of an object to exhibit more than one form with the same name. For understanding: • One name → multiple forms • One variable name → different values • One method name → different behaviour

Types of Polymorphism

  1. Compile Time Polymorphism (Static Binding)
  2. Runtime Polymorphism (Dynamic Binding)

1. Compile Time Polymorphism

If the binding is achieved at compile time and the same behaviour is executed at runtime, it is known as compile-time polymorphism. • It is also known as Static Polymorphism. • Ways to achieve:

  • Method Overloading
  • Constructor Overloading
  • Variable Shadowing
  • Method Shadowing
  • Operator Overloading (Not supported in Java)

A. Method Overloading If more than one method is created with the same name but different arguments in the same class. • Different Formal Arguments:

  • Differ in number of arguments.
  • Differ in types of arguments.
  • Differ in order of arguments.
  • Example: java.io.PrintStream has println(), println(int), println(String).

B. Constructor Overloading Class having more than one constructor with different formal arguments.

C. Method Shadowing If a subclass and superclass have a static method with the same declaration but different implementation. • Binding: Done at compile-time based on Reference Type (not Object Type). • Rules:

  • Return type must be same (or covariant).
  • Access modifier same or higher visibility.
  • Only for static methods.

D. Variable Shadowing If superclass and subclass have a variable with the same name but different values. • Binding: Done at compile-time based on Reference Type. • Applies to both static and non-static variables.

2. Runtime Polymorphism

If binding occurs at compile time but different behaviour is achieved at runtime. • Also known as Dynamic Binding. • Achieved by Method Overriding.

Method Overriding If subclass and superclass have a non-static method with the same declaration but different implementation. • Rules:

  • IS-A relationship (Inheritance) is mandatory.
  • Only for non-static methods.
  • Signature must be exactly the same.
  • Return Type: Same or Covariant (Subclass return type can be child of parent's return type).
  • Access Modifier: Same or higher visibility.
  • @Override Annotation: Recommended to ensure correct overriding. • Execution: Depends on the Object Created (Runtime object).