GENERALIZATION
Program in JAVA:
(other members are eliminated for better understanding..)
package generalization;public interface Distance
{
public void go();
}
class OnCycle implements Distance
{
public void go()
{
System.out.println("covered 2 km on cycle...");
}
}
class OnBike implements Distance
{
public void go()
{
System.out.println("covered 5 km on bike...");
}
}
class OnCar implements Distance
{
public void go()
{
System.out.println("covered 7 km on car...");
}
}
class Generalization
{
public static void move(Distance d1) //Generalized method
{
d1.go(); //which uses up casting process
}
public static void main (String [] args)
{
move(new OnCycle()); //Distance d1= new OnCycle();
move(new OnBike()); //Distance d1= new OnBike();
move(new OnCar()); //Distance d1= new OnCar();
}
}
No comments:
Post a Comment