Cortez Ninja
Cortez Ninja

Reputation: 89

Java: OOP concepts, Abstract class, interface

Task 1. Design and implement classes SportsClub (abstract class), FootballClub. Classes should include appropriate methods and hold information about name of the club, its location and various statistics about the club. FootballClub should include statistics such as how many wins, draws and defeats an instance of it has achieved in the season, the number of goals received and scored. The number of points that a club currently has, and number of matches played.

Task 2. Implement a class PremierLeagueManager which extends interface LeagueManager. PremierLeagueManager class maintains a number of football clubs which play in the premier league. The class should create a menu based on text input and give the user the choice of: •

  1. Create a new football club and add it in the premier league. •

  2. Delete (relegate) an existing football club from the premier league

  3. Display the various statistics for a selected club.
  4. Display the Premier League Table

I designed abstract class

public abstract class SportsClub {
    int position;
    String name;
    int points;
    int wins;
    int defeats;
    int draws;
    int totalMatches;
    int goalF;
    int goalA;
    int goalD;
    String location;
}

I extended the abstract class I know I can use the get/set method but I used contructor to initialise (correct me if I'm worng)

public class FootballClub extends SportsClub {

     FootballClub(int position, String name, 
                  int points, int wins, 
                  int defeats, int draws, 
                  int totalMatches, int goalF, 
                  int goalA, int goalD) {
         this.position = position;
         this.name = name;
         this.points = points;
         this.wins = wins;
         this.defeats = defeats;
         this.draws = draws;
         this.totalMatches = totalMatches;
         this.goalF = goalF;
         this.goalA = goalA;
         this.goalD = goalD;     
     }
}

Now I need to write an interface which I know how to do it but I'm trying to get my head around that am I declaring the methods inside interface or shall i do it in abstract class. Basically I need a help how I can approach to this whole excersie. Dont know what class I should be using for main method. I'm a new learner and really confused if somebody help me out on that about how should I approach I'm sure I will be able to do it myself. Not asking for code need help if somebody simplify me this whole excerise please

Upvotes: 0

Views: 902

Answers (1)

mprivat
mprivat

Reputation: 21902

First a comment on the existing code. I would that constructor in the abstract superclass since it initializes the fields from that class.

public abstract class SportsClub {
    SportsClub(int position, String name, int points, int wins, int defeats, int draws, int totalMatches, int goalF, int goalA, int goalD){
        this.position = position
        ...

Then

public class FootballClub extends SportsClub{

   FootballClub(int position, String name, int points, int wins, int defeats, int draws, int totalMatches, int goalF, int goalA, int goalD) : base(position, name, points, wins, defeats, draws, totalMatches, goalF, goalA, goalD) {
   }

That makes it more reusable for the next subclasses. Also as a general rule, whenever I see this many parameter to a method/constructor, I create a struct/class to contain them all (say ClubInfo for example). It's less verbose that way and the API of your classes are easier to read.

Now on to your question. One basic difference between an abstract class and an interface is that the interface is 100% abstract and has no state (i.e. no fields). So since your SportsClub has all these fields, it holds a state, and therefore needs to be a class. Since it isn't a complete implementation (presumably you'll add more methods that the actual sports club need to implement because it's tied to that particular sport), it needs to be abstract.

For example. Any sports club, regardless of the sport, can send an advertisement about a game.

So in SportsClub you add the method: public abstract string AnnounceGame();

Now, how the game is announced varies by sport:

So FootballClub might implement it this way:

public string AnnounceGame() { return "Come join us at the stadium"; }

While BaseballClub might do:

public string AnnounceGame() { return "Come join us at the ball park"; }

The basic idea is that if you're a sport promoter class, for example, you don't care what type of sports club you have, you know that any SportsClub can make announcements.

SportsClub clubA = new FootballClub();
SportsClub clubB = new BaseballClub();

If I call:

clubA.AnnounceGame(); // outputs "Come join us at the stadium"
clubB.AnnounceGame(); // outputs "Come join us at the ball park"

So we've used inheritance to reduce complexity. The promoter doesn't have to care what type of sports club instance it has.

Upvotes: 2

Related Questions