0% found this document useful (0 votes)
25 views9 pages

4.java Polymorphism

java polymorphism concept

Uploaded by

sumityadavit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views9 pages

4.java Polymorphism

java polymorphism concept

Uploaded by

sumityadavit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Polymorphism

Polymorphism in java is a concept by which we can perform a single action by


different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The
word "poly" means many and "morphs" means forms. So polymorphism means many
forms.
For example, you have a smart phone for communication. The communication
mode you choose could be anything. It can be a call, a text message, a picture
message, mail, etc. So the goal is common that is communication, but their approach
is different. This is called Polymorphism
There are two types of polymorphism in java:
 compile time polymorphism
 runtime polymorphism
We can implement the above two in java by method overloading and method
overriding respectively.

A) Compile Time Polymorphism or Static Polymorphism:


It can be implemented by using Method Overloading in java. Compile time
polymorphism is a process in which a call to an overloaded method is resolved at
compile-time.
If a class have more than one method with same name but difference in the
parameters and we called one of the method, then it create some confusion which
method is called. If it is resolved by identifying suitable method based on number of
arguments, types arguments and order of arguments at compile time only, then it is
called as compile time polymorphism.

Method Overloading:
Method Overloading means different methods to have same name, but different
signatures. Overloading is related to compile time (or static) polymorphism.

There are three ways to overload the method in java

 By changing number of arguments

 By changing the data type of arguments

 By changing the order of arguments


Example:

class Display
{
public void display()
{
System.out.println ("Inside First display method");
}

public void display(String val)


{
System.out.println ("Inside Second display method, value is:
"+val);
}

public void display(String val1,String val2)


{
System.out.println ("Inside Third display method, values are:
"+val1+" "+val2);
}
}
public class Test
{
public static void main (String args[])
{
Display a = new Display();
//Calls the first display method
a.display();
//Calls the second display method
a.display("Second Campus");
//Calls the third display method
a.display("Second Campus", "Learning Solutions");
}
}

Output:

Inside First display method


Inside Second display method, value is: Second Campus
Inside Third display method, values are: Second Campus Learning Solutions
Constructor Overloading:
It means, a class contains more than one constructor with different signatures.
As like method overloading, constructors can also be overloaded.
Overloaded constructor is called based upon the parameters specified at the
time of object creation. The compiler differentiates these constructors by taking into
account the number of parameters in the list and their type.\

Example:
class A{
A(){
System.out.println("No argument constructor");
}
A(int x){
System.out.println("One argument constructor");
}
A(int x, int y){
System.out.println("Two argument constructor");
}
}
public class Test{
public static void main(String[] as){
A a1= new A();
A a2= new A(1);
A a3= new A(2,4);
}
}
Output:
No argument constructor
One argument constructor
Two argument constructor

B) Runtime Polymorphism or Dynamic Polymorphism:


It can be implemented by using Method Overloading through inheritance in
java. Runtime polymorphism is a process in which a call to an overridden method is
resolved at runtime rather than compile-time.

If a method in parent class is overridden in child class, and then if we call that
method then JVM will firstly checks that method is existed in reference variable type
and It doesn’t check about the right version which is currently pointing to. This task
will be postponed to the programs runtime. At runtime, JVM checks which object is
currently referring the object and calls right version of that object. This is called
Runtime Polymorphism.
Method Overriding:
If subclass (child class) has the same method as declared in the parent class, it
is known as method overriding.
Or
If subclass provides the specific implementation of the method that has been
provided by one of its parent class is called method overriding.
Usage of Method Overriding:
 Method overriding is used to provide specific implementation of a
method that is already provided by its super class.

 Method overriding is used for runtime polymorphism

Rules for Method Overriding:


 Method must have same name as in the parent class

 Method must have same parameter as in the parent class.

 Must be IS-A relationship (inheritance).

Example:
class Parent {
public void display()
{
System.out.println ("display() method of Parent class");
}
}
class Child extends Parent
{
public void display()
{
System.out.println ("display() method of child class");
}
}
public class Test{
public static void main(String args[])
{
//Assign Parent reference to Parent class
Parent p = new Parent();
p.display();
//Assign Child reference to Child class
Child c=new Child();
c.display();
}
}
Output:
display() method of Parent class
display() method of child class

Dynamic Binding or Dynamic Method Dispatch:


Dynamic Method Dispatch is a technique in which the overriden method to call
is resolved at the run-time rather than at compile time. In this technique we will
assigning the Child object to the Parent class reference.
Example1:
class Parent
{
public void display()
{
System.out.println ("display() method of Parent class");
}
}
class Child extends Parent
{
public void display()
{
System.out.println ("display() method of Child class");
}
public void show(){
System.out.println ("show() method is called");
}
}
public class Test{
public static void main(String args[])
{
//Declaring Reference variable
Parent p;
//Assign Parent reference to Parent class Object
p=new Parent();
p.display();
//Assign Parent reference to Child class Object
p= new Child();
p.display();
//The method show() is undefined for the type Parent.
//If called using Dynamic binding then it show error
//p.show(); //Invalid

}
}

Output:
display() method of Parent class
display() method of Child class

In the above example display() 2 times called in main method and one time it
called Parent class version and next time it called Child version.
The compiler checks, the reference variable is of type Parent class and checks
display() is exist in Parent class or not. It doesn’t check about the right version which
is currently pointing to. This task will be postponed to the programs runtime. At
runtime, JVM checks which object is currently referring the object and calls right
version of that object. This is called Runtime Polymorphism. The choosing of current
version of method is happened at run time rather than at compile time. So, it is called
“Dynamic Method Dispatch” Or “Dynamic Binding” Or “Late Binding”.

Example2:
class Parent
{
public void display()
{
System.out.println ("display() method of Parent class");
}
}
class Child extends Parent
{
public void display()
{
System.out.println ("display() method of Child class");
}
public void show(){
System.out.println ("show() method is called");
}
}
public class Test{
public static void main(String args[])
{
//Declaring Reference variable
Parent p;
//Assign Parent reference to Parent class Object
p=new Parent();
p.display();
//Assign Parent reference to Child class Object
p= new Child();
p.display();
//The method show() is undefined for the type Parent.
//If called using Dynamic binding then it show error
//p.show(); //Invalid
Child c=new Child();
c.display();
c.show();
}
}
Output:
display() method of Parent class
display() method of Child class
display() method of child class
show() method is called
We cannot call the Child class methods (which are not defined in Parent class)
with Parent class reference because at compile time JVM checks whether the called
method is present in reference (on which it is called) type class or not.

final Keyword:
The final keyword in java is used to restrict the user. The java final keyword
can be used in many contexts. Final can be:
 variable
 method
 class

A) final Variable:
If you make any variable as final, you cannot change the value of final variable
(It will be constant). It is good practice to represent final variables in all uppercase,
using underscore to separate words.
The final keyword can be applied with the variables, a final variable that have
no value it is called blank final variable or uninitialized final variable. It can be
initialized in the constructor only. The blank final variable can be static also which
will be initialized in the static block only.
Example:
final int MAX=23;//final variable
B) final Method:
When a method is declared with final keyword, it is called a final method. A
final method cannot be overridden. The Object class does this—a number of its
methods are final. We must declare methods with final keyword for which we
required to follow the same implementation throughout all the derived classes.
Example:
final void method(){
//method definition
}
C) final Class:
When a class is declared with final keyword, it is called a final class. A final
class cannot be extended (inherited).
Example:
final class A{
//methods and fields
}

this Keyword:
Keyword 'this' is a reference variable that refers to the current object.

Usage of this keyword:


Here is given the 6 usage of java this keyword.
1) this can be used to refer current class instance variable:
The this keyword can be used to refer current class instance variable. If there is
ambiguity between the instance variables and parameters, this keyword resolves the
problem of ambiguity.
Example:
class Student{
int rollno;
String name;
Student(int rollno,String name){
this.rollno=rollno;
this.name=name;
}
}
In the above example, parameters (formal arguments) and instance variables are
same. So, we are using this keyword to distinguish local variable and instance
variable.
2) this can be used to invoke current class method (implicitly):
You may invoke the method of the current class by using the this keyword. If
you don't use the this keyword, compiler automatically adds this keyword while
invoking the method.
Example:
class A{
void m(){
System.out.println("hello m");
}
void n(){
System.out.println("hello n");
//m();//same as this.m()
this.m();
}
}
3) this() can be used to invoke current class constructor:
The this() constructor call can be used to invoke the current class constructor. It
is used to reuse the constructor. In other words, it is used for constructor chaining.

Example:
class A{
A(){System.out.println("hello a");}
A(int x){
this();
System.out.println(x); }
A(int x, int y){
this(x);
System.out.println(y);}}
4) this can be passed as an argument in the method call:
The this keyword can also be passed as an argument in the method. It is mainly
used in the event handling.
5) this can be passed as argument in the constructor call:
We can pass this keyword in the constructor also. It is useful if we have to use
one object in multiple classes.
6) this can be used to return the current class instance from the method:
We can return this keyword as an statement from the method. In such case,
return type of the method must be the class type (non-primitive).
Syntax:
return_type method_name(){
return this;
}

You might also like