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.


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;

	public Student() {


	//public get & set
	public String getName(){

	public void setName(String a){ = a;

	public String getId(){

	public void setId(String b){ = 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.setUnderGradDeg("B.A. Account");

		u1.setHighSchool("EVEREST SCHOOL");


		//for print

		for(Student a : studentAll){



//————-in class Graduate

public class Graduate extends Student{

	private String underGraduateDegree;
	private String underGraduateInstitute;

	public Graduate() {

	//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(){
		System.out.println("Graduate: "
			+ this.getUnderGradDeg()
		+"nInstitute: " + this.getUnderGradIns());

//——————in undergraduate class
public class UnderGraduate extends Student{

	private String highSchool;

	public UnderGraduate() {

	//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(){
		System.out.println("HighSchool: "
			+ this.getHighSchool());


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){

      or go for generics.

Leave a Reply

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

You are commenting using your 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: