Monday, February 6, 2017

Polymorphism and its type-Java

Hello guys , today we will read about one important characteristice of oops i.e Polymorphism

--polymorphism---

One thing in many form
or
The ability to appear in more than one form

In Java there are 2 type of polymorphism
-compiler time polymoriphism (static binding) -achieved by method overloading

-runtime polymorphism(dynamic binding) -achieved by method overriding

---Overloading---------------
there are 2 type of overloading in java
-method overloading
-constructor overloading
-operator overloading

-------------------------------Method Overloading----------------------------------
If the class contains more than one method with same name and different no. of arguments(return type is not consider in overloadding) or same no. of argument with different data types.
Ex -
class c{
void m1(int a)
void m1(int a , int b)
void m2(char c)
void m2(String str)

}
Method signature - method name and paramters is metehod signature
Real time example-
for Adhar Card we have few options to get the duplicate adhar card
we can provide- dob and name
pan no
name and father name
so here we will use method overloading as we dont know what the user is going to give to fetch its information.

--------------------------------Constructor Overloading--------------
Same constructor name with different no. of arguments or same constructor with same no. of arguments with different data type.

Ex-
class Test
{
Test(int i)
{
syso("one arg constructor with int");
}
Test(int i, int k)
{
syso("2 arg constructor with int");
}
Test(char cs)
{
syso("one arg constructor with char");

}
main()
{
Test t=new Test(1);
Test t1 = new Test(1,2);
Test t3 =new Test('r');

}
}
------------------operator overloading--------------
one operator with more than one behaviour
no java is not supported operator overloading

Only one operator + is defined in java implisitly

Root class of all class is object class
Default package in java is java.lang package


-----------------------------Method Overriding-----------------------------------
To Achieve the overriding we required 2 java classes with inheritance concept

Overriden class is the parent class whose method is going to override
overriding class is the child class whose method will override the parent method
There are some rules of Method Overriding
1) Overridden method signature and child class method signature must be same
2) Return type of both the method must be same at primitive level
3)co varint return type in which the parent class has parent return type and class method is having the sub type of parent return type
4) if a method is declared as final overriding is not possible
-final class methods are final(bcz if it is not a final we will be able to override) but variable is not final
5) static method can't be override
6)child class is not able to hold the parent reference object
ex - Child child = new Parent() // invalid


Hope these points will help you in your preparation .

Thanks for reading
-Noeik



0 comments:

Post a Comment