Menu
Topics Index
...
`


Abstract Class And Methods >
Siva Nookala - 15 Mar 2016
This program shows a class hierarchy of the various players in a team like Batsmen, Bowler and WicketKeeper.

Cricket Players Class Hierarchy
class CricketPlayers
{
    public static void main(String arg[])
    {
        Player players[] = new Player[11];
        
        players[0] = new Batsmen("Sachin", 250, 11324, 100, 125);
        players[1] = new WicketKeeper("Dhoni", 153, 6021, 120, 67);
        players[2] = new Batsmen("Shewag", 110, 4341, 22, 40);
        players[3] = new Batsmen("Yuvraj", 105, 6533, 15, 46);
        players[4] = new Batsmen("Kohli", 75, 4003, 25, 60);
        players[5] = new Batsmen("Raina", 34, 2600, 12, 19);
        players[6] = new Batsmen("Rohit", 25, 1500, 5, 9);
        players[7] = new Bowler("Harbhajan", 189, 1500, 320, 4);
        players[8] = new Bowler("Zaheer", 150, 900, 220, 4);
        players[9] = new Bowler("Umesh", 25, 150, 105, 2);
        players[10] = new Bowler("Aswin", 15, 200, 60, 2);
        
        for(Player player : players)
        {
            player.print();
            System.out.println();
            System.out.println("---------------------");
        
        }    
    }
}

abstract class Player
{
    String name;
    int matchesPlayed;
    int runsScored;

    Player(String name, int matchesPlayed, int runsScored)
    {
        this.name = name;
        this.matchesPlayed = matchesPlayed;
        this.runsScored = runsScored;
    }
    
    
    void bat()
    {
    }

    void makeSomeRuns()
    {
    }

    void print()
    {
        System.out.print(name  + " played " + matchesPlayed + " matches and scored " + runsScored + " runs.");
    }
}

class Batsmen extends Player
{
    int numberOfCenturies;
    int numberOfHalfCenturies;

    Batsmen(String name, int matchesPlayed, int runsScored, int numberOfCenturies, int numberOfHalfCenturies)
    {
        super(name, matchesPlayed, runsScored);
        this.numberOfCenturies = numberOfCenturies;
        this.numberOfHalfCenturies = numberOfHalfCenturies;
    }

    void openInnings()
    {
    }

    void makeCentury()
    {
    }
    
    void makeHalfCentury()
    {
    }

    void print()
    {    
        super.print();
        System.out.print(" He is a strong batsmen and made " + numberOfCenturies +  " centuries and " + numberOfHalfCenturies + " half centuries.");
    }
}


class Bowler extends Player
{
    int numberOfWickets;
    int numberOf5WicketInnings;
    
    Bowler(String name, int matchesPlayed, int runsScored, int numberOfWickets, int numberOf5WicketInnings)
    {
        super(name, matchesPlayed, runsScored);
        this.numberOfWickets= numberOfWickets;
        this.numberOf5WicketInnings = numberOf5WicketInnings;
    }

    void openInnings()
    {
    }

    void bowlYorkers()
    {
    }
    
    void takeWickets()
    {
    }

    void print()
    {    
        super.print();
        System.out.print(" He is also a good bowler and has taken " + numberOfWickets + " wickets. He has " + numberOf5WicketInnings + " 5WI(5-Wicket Innings) in his account.");
    }

}

class WicketKeeper extends Player
{
    int numberOfCatches;
    int numberOfStumpings;
    
    WicketKeeper(String name, int matchesPlayed, int runsScored, int numberOfCatches, int numberOfStumpings)
    {
        super(name, matchesPlayed, runsScored);
        this.numberOfCatches = numberOfCatches;
        this.numberOfStumpings = numberOfStumpings;
    }

    void keepWickets()
    {
    }
    
    void stumpBatsmen()
    {
    }
    
    void makeAppeals()
    {
    }

    void print()
    {    
        super.print();
        System.out.print(" He also keeps the wickets and has " + numberOfCatches + " catches and " + numberOfStumpings + " stumpings in his account.");
    }
}


OUTPUT

Sachin played 250 matches and scored 11324 runs. He is a strong batsmen and made 100 centuries and 125 half centuries.
---------------------
Dhoni played 153 matches and scored 6021 runs. He also keeps the wickets and has 120 catches and 67 stumpings in his account.
---------------------
Shewag played 110 matches and scored 4341 runs. He is a strong batsmen and made 22 centuries and 40 half centuries.
---------------------
Yuvraj played 105 matches and scored 6533 runs. He is a strong batsmen and made 15 centuries and 46 half centuries.
---------------------
Kohli played 75 matches and scored 4003 runs. He is a strong batsmen and made 25 centuries and 60 half centuries.
---------------------
Raina played 34 matches and scored 2600 runs. He is a strong batsmen and made 12 centuries and 19 half centuries.
---------------------
Rohit played 25 matches and scored 1500 runs. He is a strong batsmen and made 5 centuries and 9 half centuries.
---------------------
Harbhajan played 189 matches and scored 1500 runs. He is also a good bowler and has taken 320 wickets. He has 4 5WI(5-Wicket Innings) in his account.
---------------------
Zaheer played 150 matches and scored 900 runs. He is also a good bowler and has taken 220 wickets. He has 4 5WI(5-Wicket Innings) in his account.
---------------------
Umesh played 25 matches and scored 150 runs. He is also a good bowler and has taken 105 wickets. He has 2 5WI(5-Wicket Innings) in his account.
---------------------
Aswin played 15 matches and scored 200 runs. He is also a good bowler and has taken 60 wickets. He has 2 5WI(5-Wicket Innings) in his account.
---------------------

DESCRIPTION

Here we have defined an abstract class Player and 3 sub-classes Bowler, Batsmen and WicketKeeper. We have added additional member variables and methods/functions in individual sub-classes. We have also created a constructor which initializes the member variables of the each player. The print method overrides the print method in the super-class Player and also add additional details for Bowler, Batsmen and WicketKeeper.
In the main method we have created a 11 player objects and initialized them with either Batsmen, Bowler or WicketKeeper. We have used the for-each loop to print the details of the each player. Please note that we have made the Player class abstract such that no object (or instance) of that class can be created.

THINGS TO TRY
  • Add additional member variable age in the Player class and print it as part of the print method. Also change all the constructors such that we can pass the age also as a parameter.
  • Create one more class AllRounder who has the properties of both Batsmen and Bowler. Observe the duplicate code between Batsmen, Bowler and the AllRounder classes.
Although the above program decently defines the classes, it has the following drawbacks:
  • It prevents a Batsmen to be both Batsmen and WicketKeeper. He has to be either Batsmen or a WicketKeeper.
  • Similarly, it prevents a Bowler to be both Bowler and Batsmen. He has to be either Bowler or a Batsmen.
  • We can not define a player who can lead as Captain. This Captain eligible player could be either a Bowler or a WicketKeeper or a Batsmen.
Java does not solve all the above problems since it does not support multiple inheritance, but it provides Interfaces which will solve this problem to certain extent.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.

Learn Java Programming from our Experts in 30 Days