TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Is java supports polymorphism? (http://forums.techarena.in/software-development/1272909.htm)

beelow 19-11-2009 10:55 AM

Is java supports polymorphism?
 
Hi friends,

I just started the java programming. I want to know that , Is java supports to the polymorphism?

Can you explain what is polymorphism and how it can be implemented in JAVA language with suitable example.

Your help greatly appreciated...

Katty 19-11-2009 11:11 AM

Re: Is java supports polymorphism?
 
Hi,

Obviously java supports polymorphism. Because JAVA is fully object oriented language.

Below details will give you the idea about polymorphism:

Polymorphism gives the ultimate flexibility in extensibility. Polymorphism describes a situation in which one name might refer to other different methods.

Basically in java supports two type of polymorphism:
1. overloading type.
2. overriding type.

When you override any methods in java, java determines the proper methods to call at the run time.

Overriding occurs when a class method has the same name and signature as a method in base class it is called as overriding.

Example of Overloading:
Quote:

Class Book{
String title;
String publisher;
float price;
setBook(String title){
}
setBook(String title, String publisher){
}
setBook(String title, String publisher,float price){
}
}
Example of Overriding:

Quote:

Class Book{
String title;
String publisher;
float price;
setBook(String title){
}
setBook(String title, String publisher){
}
setBook(String title, String publisher,float price){
}
}

kelfro 19-11-2009 11:22 AM

Re: Is java supports polymorphism?
 
Polymorphism is the one of the basic feature of the OOP(Object Oriented Programming).

And I think you are not aware that java is object oriented language,So there is no doubts that java supports polymorphism concept.

I don't have that much knowledge about this concept But I know that "polymorphism means when an entity behaves entirely different depends upon the context in which it is being used."

Zecho 19-11-2009 11:30 AM

Re: Is java supports polymorphism?
 
Hello friends,

Polymorphism stands for Poly(Many) + morhism(foru) which means that multiple forms.
It is an Object oriented language feature through which multiple things can be done with only one program code by changing its input parameters only.

Java supports polymorphism concept through method overloading and method overriding.

refer following example:

Quote:

public class MountainBike extends Bicycle{
private String suspension;

public MountainBike(int startCadence, int startSpeed, int startGear, String suspensionType){
super(startCadence, startSpeed, startGear);
this.setSuspension(suspensionType);
}

public String getSuspension(){
return this.suspension;
}

public void setSuspension(String suspensionType){
this.suspension = suspensionType;
}

public void printDescription(){
super.printDescription();
System.out.println("The MountainBike has a " + getSuspension()
+ " suspension.");
}
}


All times are GMT +5.5. The time now is 06:05 PM.