We can use the objects sub-class type, where ever we have super-class type. This program shows how to create a method to compare the entertainments. The Entertainments example discussed in
Java Class Inheritance is modified to demonstrate it.
Compare Entertainments Using Methods
class CompareEntertainmentsUsingMethods
{
public static void main(String s[])
{
Movie julai = new Movie();
julai.name = "Julai";
julai.director = "Trivikram";
julai.stuntMaster = "Peter Hein";
julai.numberOfArtists = 57;
julai.releaseDate = "15-Aug-2012";
julai.collectionsFirstWeek = 215467.8;
julai.collectionsRestOfTheDays = 541132.5;
Drama ramayan = new Drama();
ramayan.name = "Ramayana";
ramayan.writer = "Valmiki";
ramayan.stageSetter = "Anjaneya";
ramayan.numberOfArtists = 200000;
ramayan.releaseDate = "17-Mar-1659 BC";
ramayan.collectionsFirstWeek = 3282937242.86;
ramayan.collectionsRestOfTheDays = 93488272349.51;
Circus jumbo = new Circus();
jumbo.name = "Jumbo";
jumbo.ringMaster = "Antony";
jumbo.numberOfArtists = 316;
jumbo.releaseDate = "16-Dec-1997";
jumbo.collectionsFirstWeek = 2123132.21;
jumbo.collectionsRestOfTheDays = 234936725.09;
compareEntertainments(julai, ramayan, jumbo);
}
public static void compareEntertainments(Entertainment ent1, Entertainment ent2, Entertainment ent3)
{
if((ent1.getTotalCollections() > ent2.getTotalCollections()) && (ent1.getTotalCollections() > ent3.getTotalCollections()))
{
System.out.println(ent1.name + " has the highest collections.");
}
else if (ent2.getTotalCollections() > ent3.getTotalCollections())
{
System.out.println(ent2.name + " has the highest collections.");
}
else
{
System.out.println(ent3.name + " has the highest collections.");
}
}
}
class Entertainment
{
String name;
int numberOfArtists;
String releaseDate;
double collectionsFirstWeek;
double collectionsRestOfTheDays;
double getTotalCollections()
{
return collectionsFirstWeek + collectionsRestOfTheDays;
}
void printEntertainment()
{
System.out.println( name + " got the following collections " );
System.out.println("First Week : " + collectionsFirstWeek);
System.out.println("Rest Of The Days : " + collectionsRestOfTheDays);
System.out.println("Total Collections : " + getTotalCollections());
System.out.println("Total Artists : " + numberOfArtists);
System.out.println("Release Date : " + releaseDate);
}
}
class Movie extends Entertainment
{
String director;
String stuntMaster;
void print()
{
printEntertainment();
System.out.println("Director : " + director);
System.out.println("Stunt Master : " + stuntMaster);
}
}
class Drama extends Entertainment
{
String writer;
String stageSetter;
void print()
{
printEntertainment();
System.out.println("Writer : " + writer);
System.out.println("Stage Setter : " + stageSetter);
}
}
class Circus extends Entertainment
{
String ringMaster;
void print()
{
printEntertainment();
System.out.println("Ring Master : " + ringMaster);
}
}
OUTPUTRamayana has the highest collections.
DESCRIPTIONHere we have created a new method compareEntertainments
in the class CompareEntertainmentsUsingMethods
. This method takes three parameters and all of them are of type Entertainment
. So this method can be used not only to compare Movie
, Drama
and Circus
as we are doing here, it can be also used for comparing Movie
, Movie
and Drama
or Circus
, Circus
and Circus
etc.,
THINGS TO TRY
- Change the order of the parameters passed to the method
compareEntertainments
, from julai
, ramayan
and jumbo
to jumbo
, ramayan
, julai
. Observe that any object can be passed to the method as long as it is of Entertainment
type.
- In the main method create a new
Movie
object lifeIsBeautiful
and new Drama
object devdas
and initialize the data. Call the method compareEntertainments
to pass the parameters devdas
, lifeIsBeautiful
and julayi
. i.e. we are calling the method with objects of type Drama, Movie and Movie respectively.