Skip to content
October 22, 2008 / Narayan

Polymorphism on three classes

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.

polymorphism

fig. Polymorphism in 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.

//————-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());
	}
}
Advertisements

3 Comments

Leave a Comment
  1. EJP / Jun 9 2009 4:09 am

    > 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.

  2. Narayan / Jun 9 2009 11:20 am

    you are genius man!

    Polymorphism is a feature of OOPL

    and it’s have several behavious with a single func

    • chandra / Sep 4 2012 7:52 pm

      for(Student a : studentAll) — will work here. change the code as
      for(Object a : studentAll){
      //—Polymorphism———-
      ((Student)a).print();
      }

      or go for generics.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: