Follow us on Facebook

Header Ads

Welcome to JAVA POint

Polymorphism in Java With Example.

                                 Polymorphism in Java With Example.

Polymorphism is one of the OOPs highlight that permits us to play out a solitary activity in an unexpected way. For instance, lets say we have a class Animal that has a strategy sound(). Since this is a conventional class so we can't give it an execution like: Roar, Meow, Oink and so forth We needed to give a nonexclusive message. 



public class Animal { 

... 

public void sound(){ 

System.out.println("Animal is making a sound"); 

Presently lets say we two subclasses of Animal class: Horse and Cat that expands (see Inheritance) Animal class. We can give the execution to a similar strategy like this: 


public class Horse broadens Animal{ 

... 

@Override 

public void sound(){ 

System.out.println("Neigh"); 

what's more, 

public class Cat expands Animal{ 

... 

@Override 

public void sound(){ 

System.out.println("Meow"); 

As you can see that in spite of the fact that we had the regular activity for all subclasses sound() yet there were various approaches to do a similar activity. This is an ideal illustration of polymorphism (highlight that permits us to play out a solitary activity in an unexpected way). It would not bode well to simply call the nonexclusive sound() strategy as every Animal has an alternate sound. Along these lines we can say that the activity this technique performs depends on the kind of item.


                                What is polymorphism in programming? 

Polymorphism is the capacity of a technique to do various things dependent on the article that it is following up on. As such, polymorphism permits you characterize one interface and have numerous executions. As we have found in the above model that we have characterized the technique sound() and have the numerous executions of it in the diverse 2 sub classes. 

Which sound() strategy will be called is resolved at runtime so the model we gave above is a runtime polymorphism model. 

Sorts of polymorphism and technique over-burdening and abrogating are shrouded in the different instructional exercises. You can allude them here: 

1. Technique Overloading in Java – This is an illustration of aggregate time (or static polymorphism).

2. Technique Overriding in Java – This is an illustration of runtime time (or dynamic polymorphism) 


3. Kinds of Polymorphism – Runtime and assemble time – This is our next instructional exercise where we have covered the sorts of polymorphism in detail. I would prescribe you to go however strategy over-burdening and abrogating prior to going however this subject. 

Lets record its total code:

Model 1: Polymorphism in Java 

Runtime Polymorphism model: 

Animal.java 

public class Animal{ 

public void sound(){ 

System.out.println("Animal is making a sound"); 

Horse.java 

class Horse broadens Animal{ 

@Override 

public void sound(){ 

System.out.println("Neigh"); 

public static void main(String args[]){ 

Creature obj = new Horse(); 

obj.sound(); 

Yield: 

Neigh 

Cat.java 

public class Cat broadens Animal{ 

@Override 

public void sound(){ 

System.out.println("Meow"); 

public static void main(String args[]){ 

Creature obj = new Cat(); 

obj.sound(); 


Yield: 

Howl 

Model 2: Compile time Polymorphism 

Technique Overloading then again is an arrange time polymorphism model. 

class Overload 

void demo (int a) 

System.out.println ("a: " + a); 

void demo (int a, int b) 

System.out.println ("an and b: " + a + "," + b); 

twofold demo(double a) { 

System.out.println("double a: " + a); 

return a*a; 

class MethodOverloading 

public static void fundamental (String args []) 

Over-burden Obj = new Overload(); 

twofold outcome; 

Obj .demo(10); 

Obj .demo(10, 20); 

result = Obj .demo(5.5); 

System.out.println("O/P : " + result); 


Here the technique demo() is over-burden multiple times: first strategy has 1 int boundary, second strategy has 2 int boundaries and third one is having twofold boundary. Which technique is to be called is dictated by the contentions we pass while calling strategies. This occurs at runtime arrange time so this sort of polymorphism is known as aggregate time polymorphism. 


Output: 

a: 10 

an and b: 10,20 

twofold a: 5.5 

O/P : 30.25

0 Comments:

Post a Comment

C language and C++ progaming