Java| OOPS| polymorphism |example  

OOPs Concepts

Polymorphism allows the software developer to reuse names as well as code. Polymorphism is a term that describes a situation where one name may refer to different methods. Polymorphism is the ability of a programming language to process object differently depending on their datatype or class.

Polymorphism is the capability of method to do different things based on the object that it is acting upon.

The meaning of the word polymorphism is one name, many forms, in java in the form of multiple methods with the same name


In compiletime polymorphism the compiler will identify which method to be invoked during compile time itself and statically link it (Static linking or static binding). Method overloading is a best example of compile time polimorphism. Method overloading occurs when several methods have same names with different method signature.overloading of method is determined at the compile time. Overloaded methods let you reuse the same method name in a class, but with different arguments (and optionally, a different return type).
The rules are simple:

" Overloaded methods must change the argument list.
" Overloaded methods can change the return type.
" Overloaded methods can change the access modifier.
" Overloaded methods can declare new or broader checked exceptions.
" A method can be overloaded in the same class or in a subclass. In other words,
if class A defines a display(int i) method, the subclass B could define a display(String s) method without overriding the superclass version that takes an int. So two methods with the same name but in different classes can still be considered overloaded, if the subclass inherits one version of the method and then declares another overloaded version in its class definition.

Legal Overloads

Let's look at a method we want to overload:

public void changeSize(int size, String name, float pattern) { }
The following methods are legal overloads of the changeSize() method:
public void changeSize(int size, String name) { }
public int changeSize(int size, float pattern) { }
public void changeSize(float pattern, String name)
throws IOException { }


class Adder {
public int addThem(int x, int y) {
return x + y;
// Overload the addThem method to add doubles instead of ints
public double addThem(double x, double y) {
return x + y;

// From another class, invoke the addThem() method

public class TestAdder {
public static void main (String [] args) {
Adder a = new Adder();
int b = 27;
int c = 3;
int result = a.addThem(b,c); // Which addThem is invoked?
double doubleResult = a.addThem(22.5,9.3); // Which addThem?

In the preceding TestAdder code, the first call to a.addThem(b,c) passes two ints to the method, so the first version of addThem()-the overloaded version that takes two int arguments-is called. The second call to a.addThem(22.5, 9.3) passes two doubles to the method, so the second version of addThem()-the overloaded version that takes two double arguments-is called.


The runtime polymorphism is achieved through function overriding. Function overriding means suppose you have two classes one is super class and another on is sub class. If both classes have a method which has the same return type as well as same number and type of argument list then the function is said to be overridden. This scenario is said to be runtime polymorphism because JVM will decide at runtime that which version of the function either the one which is super class or the one which is in sub class.

Example see the following code

public class Doctor {
public void details() {
System.out.println("Doctor without any specialization");

class Dentist extends Doctor {
public void details() {

System.out.println("Doctor with specialization: Dentist ");


both the classes have details() method. But which version it displays will be decided based on the object type. The Doctor class creator might have decided that for the purposes of polymorphism, all Doctor subtypes should have a details() method defined in a unique, specific way. Polymorphically, when someone has an Doctor reference that refers not to Doctor instance, but to a Doctor subclass instance (here Dentist), the caller should be able to invoke details() on the Doctor reference, but the actual runtime object (say, a Dentist instance) will run its own specific details() method.

Main method for above two classes

public class Test {
public static void main (String [] args) {
Doctor d = new Doctor();
Doctor b = new Dentist(); //Doctor ref, but a Dentist object
d.details(); // Runs the Doctor version
b.details(); // Runs the Dentist version

The variable b is of type Doctor, which has a public details () method. But remember that at runtime, Java uses virtual method invocation to dynamically select the actual version of the method that will run, based on the actual instance. A Doctor reference can always refer to a Dentist instance, because Dentist IS-A Doctor. What makes that superclass reference to a subclass instance possible is that the subclass is guaranteed to be able to do everything the superclass can do and can also do the things which are specific to the subclass.. The following rules are applied when you are implementing function overriding.

" The argument list must exactly match that of the overridden method. If they don't match, you can end up with an overloaded
" The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass.
" The access level can't be more restrictive than the overridden method's. That is the super class method's access level should always either public or protected but it should not be private. Because private methods cannot be inherited

" The access level can be less restrictive than that of the overridden method.
" Instance methods can be overridden only if they are inherited by the subclass.
" A subclass within the same package as the instance's superclass can override any superclass method that is not marked private or final.
" You cannot override a method marked final.
" You cannot override a method marked static.
" If a method can't be inherited, you cannot override it. Remember that overriding implies that you're reimplementing a method you inherited



public class Base

public void display(String s)

System.out.println("String:"+s+" Displayig from Superclass");



public class Sub1 extends Base
public void display(String d)

System.out.println("String"+d+" Displaying from Sub1 class");


public void details()

System.out.println("This is subclass1");



public class Sub2 extends Base
public void display(String g)
System.out.println("String:"+g+" Displaying from Sub2 class");

public void prnt()
System.out.println("This is another method of Sub2 class");



public class MainClass
public static void main(String args[])

String h ="Hello";
Base b= new Base(h);
b.display();// executes base class version of display() method

Sub1 s1=new Sub1(h);
s1.display();//executes sub1 class display() method
s1.details(); // executes details() method

Sub2 s2=new Sub2();
s2.display(h);//executes sub2 version of display()

b=new Sub1();
b.display(h);// executes sub1 class display() method
//b.details(); compiler error since base class doest know about details() method so it will not execute
b=new Sub2();
b.display(h);// executes sub2 class display() method



Output of the program is as follows:
PRINT NAME: Venu Gopal Darur