public abstract class Car
{
private String model;
public abstract void color();
public String getModel()
{
return model;
}
public void setModel(String modelName)
{
model = modelName;
}
}
?
public class Honda extends Car
{
public void color()
{
System.out.println("red");
}
}
?
public class Ford extends Car
{
public void color()
{
System.out.println("blue");
}
}
?
public class MyCars
{
public static void main(String[] args)
{
Honda myHonda = new Honda();
Ford myFord = new Ford();
myHonda.setModel("My Honda is ");
myFord.setModel("My Ford is ");
System.out.print(myHonda.getModel());
myHonda.color();
System.out.print(myFord.getModel());
myFord.color();
}
}
?
In the above code, the myHonda.getModel() and myHonda.color() method calls produce different output from when the same methods are used with myFord. Explain why this is the case.
What will be an ideal response?
When you create Ford and Honda objects, each is a Car with access to the Car class getModel() and setModel() methods. Each uses its own color() method appropriately. The myFord.getModel() and myFord.color() method calls produce different output from when the same method names are used with myHonda.Using the same method name to indicate different implementations is polymorphism. Using polymorphism, one method name causes different and appropriate actions for diverse types of objects.
You might also like to view...
A(n) ____ is a small filled circle or similar icon.
A. bullet B. pointer C. token D. marker
When you save a(n) ________, it is saved as a picture file
A) screen shot B) image C) screen print D) snip
____ network is another name for a network running TCP/IP.
A. HTTP B. TCP C. UDP D. IP
Although some publications allow the use of “photo illustrations,” others have strict rules about digital manipulation—especially for news photojournalists. _________________________
Answer the following statement true (T) or false (F)