top of page

Polymorphism in Java

Polymorphism (many-forms) is one of important concepts of Object Oriented Programming (OOP) after after encapsulation and inheritance in Java. An idea behind ploymorphism is, a single form( Bike) can behave like RoadBike, CityBike or MountainBike(many forms)

When you express the “IS - A” relationship using inheritance, you create a subclass, which is a more specific type of the super class. For example, a MountainBike is a specific type of Bike.

Bike bike;

bike = new MountainBike(); bike = new CityBike(); bike = new RoadBike();

Here, the object references of different sub classes are assigned to the variable of super type. . Since the MountainBike class inherits from the Bike class , an object of "MountainBike" class IS-A object of Bike class. Such an assignment (from subclass to superclass) is called upcasting or widening conversion and it is always allowed in Java. And "upcasting" is used to write polymorphic code (check the code above) that works with classes that exists and classes that will be added in future.

At run time, objects of a derived class (sub classes) will be treated as objects of a super class. That is single form (Bike) behaves differently when you assign an object references of different sub classes or derived classes.

Bicycle.java

package polymorphism.runtime;

public class Bicycle {

private int gear;

private int speed;

private String color;

private int cadence;

// the Bicycle class has one constructor

public Bicycle(int startCadence, int startSpeed, int startGear) {

gear = startGear;

cadence = startCadence;

speed = startSpeed;

// setCadence(startCadence);

}

public int getSpeed() {

return speed;

}

public void setSpeed(int speed) {

this.speed = speed;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public int getCadence() {

return cadence;

}

public void setCadence(int newValue) {

if(newValue > 0) {

cadence = newValue;

}

else

throw new IllegalArgumentException("No negative values allowed");

}

public void setGear(int newValue) {

gear = newValue;

}

public int getGear() {

return gear;

}

/*

printDescription method could be added to the class that displays all the data currently

stored in an instance.

*/

public void printDescription() {

System.out.println("\nBike is " + "in gear " + this.gear + " with a cadence of " + this.cadence + " and travelling at a speed of " + this.speed + ". ");

}

}

MountainBike.java

package polymorphism.runtime; /* For MountainBike, add a field for suspension, which is a String value that indicates if the bike has a front shock absorber, Front. Or, the bike has a front and back shock absorber, Dual. */ 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); suspension = suspensionType; setCadence(startCadence); } public String getSuspension(){ return this.suspension; } public void setSuspension(String suspensionType) { //write... this.suspension = suspensionType; } // Note the overridden printDescription method.... public void printDescription() { super.printDescription(); System.out.println("The " + "MountainBike has a" + getSuspension() + " suspension."); } } RoadBike.java

package polymorphism.runtime;

public class RoadBike extends Bicycle{

// Add an attribute to track the tire width..

private int tireWidth;

public RoadBike(int startCadence,

int startSpeed,

int startGear,

int newTireWidth){

super(startCadence, startSpeed, startGear);

// this.setTireWidth(newTireWidth);

tireWidth = newTireWidth;

setCadence(startCadence);

}

public int getTireWidth(){

return this.tireWidth;

}

public void setTireWidth(int newTireWidth){

this.tireWidth = newTireWidth;

}

// Note that once again, the printDescription method has been overridden. This time,

//information about the tire width is displayed.

public void printDescription(){

super.printDescription();

System.out.println("The RoadBike" + " has " + getTireWidth() +

" MM tires.");

}

}

TestBikes.java

package polymorphism.runtime; public class TestBikes { public static void main(String[] args){ Bicycle bike; // bike01 = new Bicycle(20, 10, 1); // bike01.printDescription(); bike= new MountainBike(20, 10, 5, "Dual"); bike.printDescription(); bike = new RoadBike(40, 20, 8, 23); bike.printDescription(); } }

Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
Recent Posts
Archive
Search By Tags
No tags yet.
bottom of page