Program Definition 10 ::
Write a program that illustrates method overriding. Class Bond is extended by ConvertibleBond. Each of these classes defines a display() method that outputs the string “Bond” or “ConvertibleBond”, respectively. Declare an array to hold six Bond objects. Initialize the elements of the array with a mix of Bond and ConvertibleBond objects. Execute a program loop to invoke the display() method of each object.
class Bond
{
public void display()
{
System.out.println("Bond");
}
}
class ConvertibleBond extends Bond
{
public void display()
{
System.out.println("ConvertibleBond");
}
}
class Prog10
{
public static void main(String args[])
{
Bond[] b = new Bond[ 6 ];
b[ 0 ] = new Bond();
b[ 1 ] = new ConvertibleBond();
b[ 2 ] = new Bond();
b[ 3 ] = new ConvertibleBond();
b[ 4 ] = new Bond();
b[ 5 ] = new ConvertibleBond();
for ( Bond bi : b )
{
bi.display();
}
}
}
/*
D:\JAVA>javac Prog10.java
D:\JAVA>java Prog10
Bond
ConvertibleBond
Bond
ConvertibleBond
Bond
ConvertibleBond
*/
0 comments :