This Polymorphism is one of the special feature of OOPL(Object oriented Programming Language). The polymorphism can perform several functions with a single method. A simple example of Polymorphism is given below according to real world.
The term polymorphism refers to the ability of two or more objects belonging to different classes to respond to exactly the same message (method call) in different class-specific ways.
[sourcecode=’java’]
//————-in class Student
import java.util.ArrayList;
public class Student {
private String name;
private String id;
private String major;
private String gpa;
//CONSTRUCTOR
public Student() {
}
//public get & set
public String getName(){
return this.name;
}
public void setName(String a){
this.name = a;
}
public String getId(){
return this.id;
}
public void setId(String b){
this.id = b;
}
public String getMajor(){
return this.major;
}
public void setMajor(String c){
this.major = c;
}
public String getGpa(){
return this.gpa;
}
public void setGpa(String d){
this.gpa = d;
}
//print method for printing details..
public void print(){
System.out.println(“nStudent Name: ”
+ this.getName()
+ “nStudent id: ” + this.getId()
+ “nMajor: ” + this.getMajor()
+ “nGpa: ” + this.getGpa());
}
public static void main (String[] args) {
Student mainObj = new Student();
ArrayList studentAll
= new ArrayList();
UnderGraduate u1 = new UnderGraduate();
Graduate g1 = new Graduate();
g1.setName(“Narayan”);
g1.setId(“0987″);
g1.setMajor(“ComputerScience”);
g1.setGpa(“3.0″);
g1.setUnderGradDeg(“B.A. Account”);
g1.setUnderGradIns(“NCCS”);
u1.setName(“Gop”);
u1.setId(“123″);
u1.setMajor(“English”);
u1.setGpa(“4.0″);
u1.setHighSchool(“EVEREST SCHOOL”);
studentAll.add(u1);
studentAll.add(g1);
//for print
for(Student a : studentAll){
//—Polymorphism———-
a.print();
}
}
}
//————-in class Graduate
public class Graduate extends Student{
//Attributes
private String underGraduateDegree;
private String underGraduateInstitute;
//Constructor
public Graduate() {
super();
}
//Get and set method
public String getUnderGradDeg(){
return this.underGraduateDegree;
}
public void setUnderGradDeg(String a){
this.underGraduateDegree = a;
}
public String getUnderGradIns(){
return this.underGraduateInstitute;
}
public void setUnderGradIns(String b){
this.underGraduateInstitute = b;
}
//for printing
public void print(){
super.print();
System.out.println(“Graduate: ”
+ this.getUnderGradDeg()
+”nInstitute: ” + this.getUnderGradIns());
}
}
//——————in undergraduate class
public class UnderGraduate extends Student{
//attributes
private String highSchool;
//constructor
public UnderGraduate() {
super();
}
//get and set method
public String getHighSchool(){
return this.highSchool;
}
public void setHighSchool(String a){
this.highSchool = a;
}
//works for student’s print
public void print(){
super.print();
System.out.println(“HighSchool: ”
+ this.getHighSchool());
}
}
[/sourcecode]
> This Polymorphism is one of the special feature of java.
No it isn’t, it is a feature of any object-oriented programming language.
> The polymorphism helps to control many function with a single object.
No it doesn’t, it allows a single function to have multiple behaviours.
Get it right please.
you are genius man!
Polymorphism is a feature of OOPL
and it’s have several behavious with a single func