0% found this document useful (0 votes)
64 views4 pages

Hemanth M: Program: (Using Run Time Polymorphism)

The document describes a Java program that implements runtime polymorphism using different product classes. The main classes are Product, Book, CD, and Scientific, with Book, CD, and Scientific extending Product. The program creates instances of Book and CD, calls their polymorphic PrintSalesPrize() methods to calculate sales prices differently based on the specific subclass, and outputs the results.

Uploaded by

SushmithaRamesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views4 pages

Hemanth M: Program: (Using Run Time Polymorphism)

The document describes a Java program that implements runtime polymorphism using different product classes. The main classes are Product, Book, CD, and Scientific, with Book, CD, and Scientific extending Product. The program creates instances of Book and CD, calls their polymorphic PrintSalesPrize() methods to calculate sales prices differently based on the specific subclass, and outputs the results.

Uploaded by

SushmithaRamesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//2013503064

HEMANTHM

4.TowriteaJavaprogramthatimplementsruntimepolymorphism.Consider
theabovementionedapplication;inthatcalculatetheregularpriceand
totalsalespriceforeachproduct.Note:Thesalespriceshouldbe
calculateddifferentlydependingontheproducttype.Overridingmethodof
objectinstanceofthesubclassshouldbeinvoked.
Program:

(UsingRunTimePolymorphism)

importjava.util.*;
importjava.lang.*;
importjava.io.*;
classProduct
{
protectedintP_ID;
protecteddoubleRegPrize;
publicProduct(intid,doublerp)
{
P_ID=id;
RegPrize=rp;
System.out.println("\nClassProductcreated\n");
}
publicvoidPrintSalesPrize()
//Overridingmethod
{
System.out.println("\nSalesPrizeofProductisnotdefined");
}
}
classBookextendsProduct
{
protectedStringBookName;
protectedStringAuthor;
protecteddoubleSalesPrize;
publicBook(Stringb,Stringa,intid,doublerp)
{
super(id,rp);
BookName=b;
Author=a;
System.out.println("\nClassBookcreated\n");
}
publicvoidPrintInfo()
{
System.out.println("\nInClassBook");
System.out.println("\nProductID:\t"+P_ID);

System.out.println("\nBookName:\t"+BookName);
System.out.println("\nAuthor:\t"+Author);
}
publicvoidPrintSalesPrize()
{
SalesPrize=RegPrize0.15*RegPrize;
System.out.println("\nRegularPrize:\t"+RegPrize);
System.out.println("\nSalesPrize:\t"+SalesPrize);
}
}
classCDextendsProduct
{
privateStringMovieName;
privatedoubleSalesPrize;
publicCD(intid,Stringname,doublerp)
{
super(id,rp);
MovieName=name;
System.out.println("\nClassCDcreated\n");
}
publicvoidPrintCDdetails()
{
System.out.println(\nInClassCD);
System.out.println("\nProductID:\t"+P_ID);
System.out.println("\nMovieName:\t"+MovieName);
}
publicvoidPrintSalesPrize()
{
SalesPrize=RegPrize0.25*RegPrize;
System.out.println("\nRegularPrize:\t"+RegPrize);
System.out.println("\nSalesPrize:\t"+SalesPrize);
}
}
classScientificextendsBook
{
privateintRackNo;
privateStringType;
privatedoubleSalesPrize;
publicScientific(Stringn,Stringa,intid,Stringt,doublerp)
{
super(n,a,id,rp);
Type=t;
System.out.println("\nClassScientificcreated\n");

}
publicvoidPrintBookType()
{
System.out.println("\nInClassScientific");
System.out.println("\nProductID:\t"+P_ID);
System.out.println("\nBookName:\t"+BookName);
System.out.println("\nAuthor:\t"+Author);
System.out.println("\nType:\t"+Type);
}
publicvoidPrintSalesPrize()
{
SalesPrize=RegPrize0.05*RegPrize;
System.out.println("\nRegularPrize:\t"+RegPrize);
System.out.println("\nSalesPrize:\t"+SalesPrize);
}
}
classPolymorphism
{
publicstaticvoidmain(String[]args)throwsjava.lang.Exception
{
Bookb1=newBook("Java","HerbertSchidt",54,758.25);
CDc1=newCD(465,"LINGAA",150.00);
b1.PrintInfo();
b1.PrintSalesPrize(); //InvokingOverridingmethodofBook.
c1.PrintCDdetails();
//InvokingOverridingmethodofCD.
c1.PrintSalesPrize();
}
}
TerminalEnd:
ClassProductcreated
ClassBookcreated
ClassProductcreated
CDclasscreated
InClassBook
ProductID:54
BookName:Java
Author:
HerbertSchidt
RegularPrize:758.25
SalesPrize:644.5125

//15%reductioninRegularCost

InClassCD
ProductID:465
MovieName:LINGAA
RegularPrize:150.0
SalesPrize:112.5

//5%reductioninRegularCost

You might also like