Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Polymorphism

Former Member
0 Likes
523

Hi friends,

can anyone give me a example of polymorphism using which "CASE" and "IF"

statements can be avoided.

John

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
484

Hi,

lets say you want to calculate the needed fuel for a trip and you have got different vehicles like car, airplane and bus. All of them need a different calculation.

You do this with case or if statement like:

case vehicle.

when bus.

bus fuel calculation.

when airplane.

airplane fuel calculation.

when car.

car fuel calculation.

endcase.

If you use Polymorphism, you will have a abstract class vehicle and three classes car, bus and airplane which inherit an abstract method calcFuel(). The abstract class vehicle can not be instantiated, so at the runtime it has do be an object of the inheriting types.

You have to implement the method calcFuel() in the classes car etc.

If you want to ask a vehicle at the runtime for the needed fuel you will do it like this:

vehicle->calcFuel().

And the vehicle can be an object of the type car, bus and airplane and you don't need a case or if statement.

i hope this helps,

Stefan Huemer

3 REPLIES 3
Read only

Former Member
0 Likes
484

Hi ,

Check this examples demo_interface ,demo_inheritance in SE38.

Read only

Former Member
0 Likes
485

Hi,

lets say you want to calculate the needed fuel for a trip and you have got different vehicles like car, airplane and bus. All of them need a different calculation.

You do this with case or if statement like:

case vehicle.

when bus.

bus fuel calculation.

when airplane.

airplane fuel calculation.

when car.

car fuel calculation.

endcase.

If you use Polymorphism, you will have a abstract class vehicle and three classes car, bus and airplane which inherit an abstract method calcFuel(). The abstract class vehicle can not be instantiated, so at the runtime it has do be an object of the inheriting types.

You have to implement the method calcFuel() in the classes car etc.

If you want to ask a vehicle at the runtime for the needed fuel you will do it like this:

vehicle->calcFuel().

And the vehicle can be an object of the type car, bus and airplane and you don't need a case or if statement.

i hope this helps,

Stefan Huemer

Read only

Former Member
0 Likes
484

hi john

This is an example for inheritance & polymorphism

public class Hourly extends Employee {

public Hourly(String name, double rate, double hours) {

super(name);

setRate(rate);

setHours(hours);

}

public void setRate(double rate) {

this.rate = rate;

}

public void setHours(double hours) {

this.hours = hours;

}

public double getRate() {

return rate;

}

public double getHours() {

return hours;

}

public double pay() {

return rate * hours;

}

public String toString() {

return super.toString() + " (rate is " + rate + " and hours are " + hours + ')';

}

private double rate;

private double hours;

}

with regards

vinayaka