Inheritance, is one of three object oriented concepts, which helps to separate out
common data and behavior (or member variables and methods) from multiple related classes. As observed in
Java Program To Compare Movies, it is necessary to remove duplicate code across classes.
Inheritance greatly helps in simplifying the code, enhancing its the re-usability and maintainability.
To achieve
inheritance, we need to create a
super class with the common code and behavior from multiple classes. Then we use the
extends
keyword to use that common code in the
sub classes. The program below is modified version of
Java Program To Compare Movies using inheritance.
Compare Entertainments Using Inheritance
class CompareEntertainmentsUsingInheritance
{
public static void main(String arg[])
{
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;
if((jumbo.getTotalCollections() > julai.getTotalCollections()) && (jumbo.getTotalCollections() > ramayan.getTotalCollections()))
{
jumbo.print();
}
else if (julai.getTotalCollections() > ramayan.getTotalCollections())
{
julai.print();
}
else
{
ramayan.print();
}
}
}
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 got the following collections
First Week : 3.28293724286E9
Rest Of The Days : 9.348827234951E10
Total Collections : 9.677120959237E10
Total Artists : 200000
Release Date : 17-Mar-1659 BC
Writer : Valmiki
Stage Setter : Anjaneya
DESCRIPTIONHere we have created a class called Entertainment
, where the common variables and methods from related classes Movie
, Drama
and Circus
are included. These classes (Movie
, Drama
and Circus
) inherit those properties (variables and methods) from the class Entertainment
. Here Entertainment
is the super-class and the Movie
, Drama
and Cirucs
are the sub-classes.
In the main
method, both the variables belonging the super-class and the sub-class can be accessed as if they belong to a single class. e.g., On the Drama
object ramayan
, we are accessing the super-class variables name
, numberOfArtists
, releaseDate
along with the sub-class (Drama
) variables writer
and stageSetter
. There is no difference in the way the super-class variables and the sub-class variables are accessed.
Similarly there is no difference in accessing the methods belonging to super-class and sub-class. Also observe that we can access the methods from super-class in the sub-class as if they are in the same class. e.g., The printEntertainment()
method of Entertainment
class is called in the print()
method of the Movie
class.
THINGS TO TRY
- Add two more integer variables -
totalNumberOfAudience
and numberOfAudienceLiked
. Create a method getLikePercentage()
to calculate the like percentage. Like Percentage = 100.0 * Number Of Audience Liked / Total Number Of Audience. Make this change in the super-class Entertainment
and see how all the three sub-classes - Movie
, Drama
and Circus
- automatically inherit those variables and methods.
- Change the
printEntertainment()
method in the Entertainment
class, to print the details of totalNumberOfAudience
, numberOfAudienceLiked
and the getLikePercentage()
. Also observe how making change in the super-class Entertainment
impacts all the sub-classes.
The classes in the above modified program are much smaller and easily maintainable compared to the previous program.
The class hierarchy of this program is as shown below.