Thursday, 20 September 2012

Java Practical - 10



    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
*/


Kindly Bookmark and Share it:

0 comments :

Post a Comment

Any Query ? any suggestion ? comment here

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT