Polymorphism is the feature of oops, it allow the user to do the single action in many different ways.this is called polymorphism.
Types of Polymorphism:
Polymorphism are classified into two types, they are:
1.Static Polymorphism
2.Dynamic Polymorphism
Example of Polymorphism:
public class Animal{
...
public void sound(){
System.out.println("Animal is making a sound");
}
}
public class Horse extends Animal{
...
@Override
public void sound(){
System.out.println("Neigh");
}
}
public class Cat extends Animal{
...
@Override
public void sound(){
System.out.println("Meow");
}
}
As you can see all the class extends Animal method,this is called polymorphism,one action is done by many several ways.Java Training Institute in Chennai
Static Polymorphism:
Static polymorphism is also known as compile time polymorphism.this is because these polymorphism is resolved in the compile time.the perfect example of static polymorphism are Method Overloading
Example of static Polymorphism
class SimpleCalculator
{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
}
public class Demo
{
public static void main(String args[])
{
SimpleCalculator obj = new SimpleCalculator();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}
Output
30
60
Method Overloading:
Method Overloading allows Core Java Training in Chennai the method to have the same name but different argument list, it is the same as constructor overloading.
3 Different ways we can overload a method:
1.No of Parameters
2.Sequence or order of parameter
3.Data type
Example of Method Overloading:
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:
a
a 10
Dynamic Polymorphism:
Run time Polymorphism is also known as dynamic polymorphism, this is because the dynamic polymorphism function the Java Training Institute in Chennai command in the run time, it is an example of Method Overriding.
Example of Dynamic Polymorphism:
class ABC{
public void myMethod(){
System.out.println("Overridden Method");
}
}
public class XYZ extends ABC{
public void myMethod(){
System.out.println("Overriding Method");
}
public static void main(String args[]){
ABC obj = new XYZ();
obj.myMethod();
}
}
Output:
Overriding Method
Method Overriding:
Method overriding allows the Core Java Training in Chennai user to override the method to one or more times, due to this feature we can able to reuse the code.
Example of Method Overriding:
class Human{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
Output:
Boy is eating
No comments:
Post a Comment