Menu
Topics Index
...
`


Interfaces, Packages and Access Control >
Siva Nookala - 14 Apr 2016
Interfaces is a powerful concept of Java which helps in achieving multiple inheritance to certain extent.

Interface is similar to a abstract class expect that all the methods in it are abstract. We can not include the method body for any of the methods in interface i.e. we can not define concrete methods in interface. The other differences between interface and class are explained in Difference Between Interfaces And Abstract Classes.
The syntax for creating an interface is

interface interface-name
{
    return-type method-name-1(parameters);

    return-type method-name-2(parameters);
}
Here interface is a keyword, interface-name is the name of the interface. The methods can be declared as shown. Please note that there is no method body enclosed in flower brackets { }.

Lets see, how the program Java Program For Cricket Players Using Class Hierarchy can be changed to use interfaces and improve the class hierarchy definition.
Cricket Players Class Hierarchy Using Interfaces
class CricketPlayersUsingInterfaces
{
    public static void main(String s[])
    {
    
        StrongBatsmen sachin = new StrongBatsmen("Sachin", 250, 11324, 100, 125);
        StrongWicketKeeper dhoni = new StrongWicketKeeper("Dhoni", 153, 6021, 120, 67);
        StrongBatsmen shewag = new StrongBatsmen("Shewag", 110, 4341, 22, 40);
        AllRounderBatsmen yuvraj = new AllRounderBatsmen("Yuvraj", 105, 6533, 15, 46);
        StrongBatsmen kohli = new StrongBatsmen("Kohli", 75, 4003, 25, 60);
        AllRounderBatsmen raina = new AllRounderBatsmen("Raina", 34, 2600, 12, 19);
        AllRounderBatsmen rohit = new AllRounderBatsmen("Rohit", 25, 1500, 5, 9);
        StrongBowler harbhajan = new StrongBowler("Harbhajan", 189, 1500, 320, 4);
        StrongBowler zaheer = new StrongBowler("Zaheer", 150, 900, 220, 4);
        StrongBowler umesh = new StrongBowler("Umesh", 25, 150, 105, 2);
        AllRounderBowler ashwin = new AllRounderBowler("Aswin", 15, 200, 60, 2);
    
        bowl(1, zaheer);
        bowl(2, umesh);
        bowl(3, ashwin);
        bowl(4, yuvraj);
        bowl(5, raina);
    }
    
    public static void bowl(int overNumber, IBowler iBowler)
    {
        System.out.println("Bowling over " + overNumber);
        System.out.println("--------------------------");
        iBowler.bowlYorkers();
        iBowler.takeWickets();
    }
}

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;
    }
    
    
    public void bat()
    {
    }

    public void makeSomeRuns()
    {
    }

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

class StrongBatsmen extends Player implements IBatsmen
{
    int numberOfCenturies;
    int numberOfHalfCenturies;

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

    public void openInnings()
    {
    }

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

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


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

    public void openInnings()
    {
    }

    public void bowlYorkers()
    {
        System.out.println("Strong Bowler " + name + " is bowling yorkers.");
    }
    
    public void takeWickets()
    {
        System.out.println("Strong Bowler " + name + " is taking wickets.");
    }

    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 StrongWicketKeeper extends Player implements IKeeper
{
    int numberOfCatches;
    int numberOfStumpings;
    
    StrongWicketKeeper(String name, int matchesPlayed, int runsScored, int numberOfCatches, int numberOfStumpings)
    {
        super(name, matchesPlayed, runsScored);
        this.numberOfCatches = numberOfCatches;
        this.numberOfStumpings = numberOfStumpings;
    }

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

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

class AllRounderBatsmen extends StrongBatsmen implements IBowler
{

    AllRounderBatsmen(String name, int matchesPlayed, int runsScored, int numberOfCenturies, int numberOfHalfCenturies)
    {
        super(name, matchesPlayed, runsScored, numberOfCenturies, numberOfHalfCenturies);
    }

    public void openInnings()
    {
    }

    public void bowlYorkers()
    {
        System.out.println("All Rounder Batsmen " + name + " is bowling yorkers.");
    }
    
    public void takeWickets()
    {
        System.out.println("All Rounder Batsmen " + name + " is taking wickets.");
    }
}

class AllRounderBowler extends StrongBowler implements IBatsmen
{

    AllRounderBowler(String name, int matchesPlayed, int runsScored, int numberOfWickets, int numberOf5WicketInnings)
    {
        super(name, matchesPlayed, runsScored, numberOfWickets, numberOf5WicketInnings);
    }

    public void openInnings()
    {
    }

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



interface IBatsmen
{
    void bat();

    void makeSomeRuns();

    void openInnings();

    void makeCentury();

    void makeHalfCentury();
}

interface IBowler
{
    void openInnings();

    void bowlYorkers();

    void takeWickets();
}

interface IKeeper
{
    void keepWickets();

    void stumpBatsmen();

    void makeAppeals();
}


interface ICaptain
{
    void selectTeam();
    
    void setField();
}
OUTPUT

Bowling over 1
--------------------------
Strong Bowler Zaheer is bowling yorkers.
Strong Bowler Zaheer is taking wickets.
Bowling over 2
--------------------------
Strong Bowler Umesh is bowling yorkers.
Strong Bowler Umesh is taking wickets.
Bowling over 3
--------------------------
Strong Bowler Aswin is bowling yorkers.
Strong Bowler Aswin is taking wickets.
Bowling over 4
--------------------------
All Rounder Batsmen Yuvraj is bowling yorkers.
All Rounder Batsmen Yuvraj is taking wickets.
Bowling over 5
--------------------------
All Rounder Batsmen Raina is bowling yorkers.
All Rounder Batsmen Raina is taking wickets.

DESCRIPTION

Here we have declared one abstract class Player, four interfaces IBatsmen, IBowler, IKeeper, ICaptain and classes StrongBatsmen, StrongBowler, StrongWicketKeeper, AllRounderBatsmen, AllRounderBowler. In the main method we have created various players and used the bowl method to bowl using a StrongBowler or AllRounderBatsmen or AllRounderBowler. As long as the class implements the interface IBowler, that class's object can be passed to the bowl method. That is the reason we were able to bowl using zaheer, umesh, aswin, yuvraj and raina who may be a StrongBowler or AllRounderBatsmen or AllRounderBowler.

THINGS TO TRY
  • Create a method void bat(int position, IBatsmen iBatsmen) in the main method and pass sachin, kohli, yuvraj, aswin, shewag as parameters. Don't forget to put some print statements in the methods makeCentury and makeHalfCentury to see the results.
  • Implement one more class WicketKeeperCaptain which extends from StrongWicketKeeper and implements the interface ICaptain. Change dhoni from StrongWikcetKeeper to WicketKeeperCaptain and call methods like setField(), selectTeam().
As shown in the above program we have defined multiple interfaces and classes which implement these interfaces. Although the classes definition is not complete, it is much better compared to the previous program.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.