top of page
Search
Writer's pictureAbhinaw Tripathi

Polymorphism Java


Polymorphism :

In java, a variable ,an object or a method can exist in different forms,thus performing various task depending on the context.because same variable and method can perform different task. So, the ability to exist in different forms is called Polymorphism.

Polymorphism with Variables:

When using variables ,some time inherently the data type of the result is decided by the compiler and accordingly execution proceeds.such as

int a=10,b=20; System.out.println(a+b);

Thus the result a+b is exhibiting polymorphism nature. So, Coercion is the automatic conversion between different data types done by the compiler.such as float a=15.5f; int x=(int)a;

What is conversion?

Ans: Conversion is an explicit change in the data type specified by the cast operator.

Polymorphism using Methods:

if the same method performs different tasks, then that method is said to exhibit polymorphism.

Types of Polymorphism:

-Static Polymorphism or Compile Time Polymorphism -Dynamic Polymorphism or Run Time Polymorphism

Dynamic Polymorphism or Run Time Polymorphism:

The Polymorphism exhibited at run time is called Dynamic polymorphism.This means when a method is called, the method call is bound to the method body at time of running the program ,dynamically . In this case Java compiler does not know which method is called at the time of compilation. Only JVM,s know at run time which method is to be executed .That is why it is called Run Time Polymorphism or Dynamic Polymorphism.

Sample with two instance methods having same name: void add (int a ,int b) { System.out.println(" sum of two:" + (a+b)); } void add (int a,int b,int c) { System.out.println("sum of three :"+ (a+b+c); }

The bodies are different So they can perform different tasks. Now,who will deceide which method to be exceuted?.is it Java compiler or JVM?. Since, the methods are called by using an object ,the java compiler can not deceide which method is actually called by user.It has to wait till the objects will be created at run time by JVM.So ,JVM will decide which methods would be get called. Now,again there is a question how JVM knows which method would be called?. Answer is for this JVM observes the signature of the methods.Methods signature consist of a method name and its parameter.then even if two methods have same name but their signature will be different. What is method Signature?

Ans : Method signature reprsents the method name along with method parameters. What happens is the JVM vlaues passed to method at the time of method call with the method signature and picks up the appropriate method.

Sample Program:

class Sample { void add (int a,int b) { System.out.println(" sum of two:" + (a+b)); } void add (int a,int b,int c) { System.out.println("sum of three :"+ (a+b+c); } } class PolyApp { public static void main(String args[]) { Sample s=new Sample(); s.add(10,15); s,add(10,15,20); } }

What is method Overloading?

Ans: Writing two or more methods in the same class in such a way that each method has same but with different method signatures---is called method overloading.

What is method overriding? Ans: writing two or more methods in super and sub classes such that the methods have same name and same signature---is called method overriding. Sample Program: class One { void calculate (double x) { System.out.println("Square value = " + (x*x)); } } class Two extends One { void calculate (double x) { System.out.println("Cube value = " + (x*x*x)); } } class OverridingApp { public static void main(String args[]) { Two t=new Two(); t.calculate(22); } }

Note:

when a super class method is overridden by the sub class method,JVM calls only the sub class method and never the super class method.

What is the difference between Overloading and Overriding?

Method Overloading :

1) Writing two or more methods with same name but with different signature is called Method Overloading.

2)Method Overloading is done in the same class.

3)In method overloading ,method return type can be same or different.

Method Overriding :

1) Writing two or more methods with the same name and same signature is called Method Overriding.

2)Method Overriding is done in super ans sub classes.

3)In method overriding ,methods return type should always be same.

Static Polymorphism :

The Polymorphism exhibited at compilation time is called Static Polymorphism.Here the java compiler knows without any ambiguity which method is called at the time of compilation.It is called static polymorphism because at the time of compilation it needs not to wait till the objects are created .Since at the time of the compilation ,the method call can be bound with actual method body,this comes under static polymorphism.

Polymorphism with Static Methods:

A static method is a method whose single copy in memory is shared by all the objects of the class.static methods belong to the clas rather than to the objects.So they are also called class methods.When static methods are overloaded or overridden ,since they do not depend on the objects,the java compiler need not wait till the objects are created to understand which method is called.

Sample Program:

class One { static void calculate (double x) { System.out.println("Square value = " + (x*x)); } } class Two extends One { static void calculate (double x) { System.out.println("Cube value = " + (x*x*x)); } } class PolyApp { public static void main(String args[]) { One o = new Two(); t.calculate(22); } }

Result: Square value = 484.0 Polymorphism with Private Methods : Private methods are the methods which are declared by using the access specifier 'private'.this access specifier makes the method not be available outside the class . So other programmers can not access the private methods.even private methods are not available in the sub classess.This means there is no posibility to override the private methods of super class in its sub class. So only method overloading is possible in case of private methods.

Can you override private methods?

Ans: No,private methods are not available in the sub classes,So they can not be overriden.

Polymorphism with Final Methods:

Methods which are declared as final are called final method.final method can not be overridden because they are not available to the sub class .So only method overloading is possible with final method.

There are two uses of declaring a method as final which are - when a method is declared as final,the performance will be better . eg. In class A ,we got a final method, method1().This method is being called from class B method().

clas A { final void method1() { System.out.println("Hello"); } }

class B { void method2() { A.method()1 ; } }

Can we take private methods and final methods as same?

Ans: Yes, Java compiler assigns the value for the private methods at the time of compilation. Also, private methods can not be modified at run time.this is the same class with final methods also.Neither the private methods nor the final methods can be overridden.So private methods can be takesn as final methods.

final Class:

A final class is a which is declared as final keyword before a class prevents inheritance.This means sub class can not be created to a final class.

eg. final class A class B extends A // invalid What is final? Ans: 1)It is used to declare constants. 2)It is used to prevent inheritance.

What is difference between dynamic and static polymorphism? Ans:

Dynamic Polymorphism :

It exhibited at run time.Here java compiler does not understand which methods is called at compilation time.Only JVM decides which method is called at run time.Method overloading and Method Overriding using instance method are examples for dynamic polymorphism.

Static polymorphism:

It exhibited at compile time.here java compiler knows which method is called.Method overloading and method overriding using static methods;method overloading using private of final methods are examples for static polymorphism.

Sample Program:

/** * */ package com.poly;

/** * @author Abhinaw.Tripathi * */ class Commercial { private String name;

public String getName() { return name; } public void setName(String name) { this.name = name; } void calculateBill(int units) { System.out.println("Customer: " + getName()); System.out.println("Bill Amount: " + units *7.00); } }

class Domestic extends Commercial { void calculateBill(int units) { System.out.println("Customer: " + getName()); System.out.println("Bill Amount: " + units *2.50); } }

public class ElectricBillApp {

public static void main(String[] args) { Commercial c=new Commercial(); c.setName("Abhinaw Tripathi"); c.calculateBill(250); Domestic d=new Domestic(); d.setName("Sandeep"); d.calculateBill(100);

}

}

Result:

Customer: Abhinaw Tripathi Bill Amount: 1750.0 Customer: Sandeep Bill Amount: 250.0

4 views0 comments

Recent Posts

See All
bottom of page