0

I'm trying to figure out how to implement multiple interfaces in my java program.

I want my program to implement a college football player class that incorporates the three classes (footballPlayer, student and person).

public class app {
public static void main(String[] args)
   { 
     student st1 = new student("Zack","Mills",22); 
     System.out.println(st1.getAllInfo()); 
     footballPlayer fp1 = new footballPlayer("Zack","Mills",22,5.9f, 240,"Junior","Running Back");
   System.out.println(fp1.getAllInfo());
   } 
 }

public class person {
 //---------Declaring attributes---- 
 private String firstName; 
 private String lastName; 
 private int age; 
 //------------------------------ 
 //----------Constructor------------ 
 person(String a, String b, int c) 
 { 
      firstName = a; 
      lastName = b; 
      age = c; 
 } 
 //---------- METHODS -------- 
 String getInfo() 
 { 
      return "NAME = "+getFirstName()+ " "+getLastName()+" "+"Age = "+ getAge(); 
 } 
 //------------------------------------------------ 
 /** 
  * @return the firstName 
  */ 
 public String getFirstName() { 
      return firstName; 
 } 
 /** 
  * @param firstName the firstName to set 
  */ 
 public void setFirstName(String firstName) { 
      this.firstName = firstName; 
 } 
 /** 
  * @return the lastName 
  */ 
 public String getLastName() { 
      return lastName; 
 } 
 /** 
  * @param lastName the lastName to set 
  */ 
 public void setLastName(String lastName) { 
      this.lastName = lastName; 
 } 
 /** 
  * @return the age 
  */ 
 public int getAge() { 
      return age; 
 } 
 /** 
  * @param age the age to set 
  */ 
 public void setAge(int age) { 
      this.age = age; 
  } 


}

public class footballPlayer extends person {

//-----------FOOTBALL PLAYER ATTRIBUTES----------------------------
private float height; 
private float weight; 
private String experience; 
private String position; 




footballPlayer(String fn, String ln, int ag,float ht, float wt, String exp, String pos) 
{ 
    super(fn, ln, ag); 
    height = ht; 
    weight = wt; 
    experience = exp; 
    position = pos; 
} 
/** 
 * @return the height 
 */ 
public float getHeight() { 
    return height; 
} 
/** 
 * @param height the height to set 
 */ 
public void setHeight(float height) { 
    this.height = height; 
} 
/** 
 * @return the weight 
 */ 
public float getWeight() { 
    return weight; 
} 
/** 
 * @param weight the weight to set 
 */ 
public void setWeight(float weight) { 
    this.weight = weight; 
} 
/** 
 * @return the experience 
 */ 
 public String getExperience() { 
     return experience; 
} 
/** 
 * @param experience the experience to set 
 */ 
public void setExperience(String experience) { 
     this.experience = experience; 
} 
/** 
 * @return the position 
 */ 
public String getPosition() { 
    return position; 
} 
/** 
 * @param position the position to set 
 */ 
public void setPosition(String position) { 
    this.position = position; 
} 
String getAllInfo() 
{ 
    return getFirstName() + " " + getLastName() + " " + getAge() + " " + " " + getHeight() + " " + getWeight() + " " + getExperience() + " " + getPosition(); 
} 

 private String status; 
}

public class student extends person {

private String status;
 student(String informedFirstName, String informedLastName, int informedAge) 
{ 
   super(informedFirstName, informedLastName, informedAge); 
   if (getAge() <= 25) status = "Traditional";

} 
String getStatus() 
{ 
   return this.status; 
} 
public void setStatus(String status) 
{ 
   this.status = status; 
} 

String getAllInfo() 
{ 
   return getFirstName() + " " + getLastName() + " " + getAge() + " " + getStatus(); 
 } 
}

 public class CollegefootballPlayer {

//attributes of football player and student

}
Olive Bassey
  • 37
  • 1
  • 2
  • 9
  • 1
    You can only implement multiple interfaces in java, you cannot extend multiple classes. To do what you want, you will need to turn your student and footballplayer into interfaces. Not related, but your code will be much easier to read (and therefore easier for us to help you) if you indent it properly and follow java naming conventions (capitalize class names). – nhouser9 Jan 29 '17 at 21:13
  • Implementing multiple interfaces != multiple inheritance – azurefrog Jan 29 '17 at 21:13
  • 2
    Let's begin with, Java doesn't support multiple inheritance from multiple objects/classes, you can extend from a single class and `implement` multiple `interfaces`. If you're clever, you can chain the class in such away that the extend one after another `Person` -> `Student` -> `FootballPlayer` – MadProgrammer Jan 29 '17 at 21:14
  • 1
    @MadProgrammer which, in turn, would disallow a ``teacher`` to be a FootballPlayer, too. – Jan B. Jan 29 '17 at 21:21
  • @Matt Maybe that's a legitimate use case, teachers not been allowed to be football players, maybe they're restricted to only been allowed to be coaches, but that would require more context. Me, personally, I'd use interfaces, then you won't have to care (so much) – MadProgrammer Jan 29 '17 at 21:24
  • @MadProgrammer i fully agree. it's hard to give advice without the full context and knowing where the model is meant to go to in the future. – Jan B. Jan 29 '17 at 21:56

1 Answers1

1

I would add to the comments as well but don't have the reputation to do so yet. Anyway, the others are correct about lack of multiple inheritance in Java. In addition to working with interfaces there is also some discussion about object composition instead of inheritance.

Here's a thread on the subject:

Advantages of composition over inheritance in Java

Composition comes with a great deal of flexibility. The member objects of your new class are typically private, making them inaccessible to the client programmers who are using the class. This allows you to change those members without disturbing existing client code. You can also change the member objects at run time, to dynamically change the behavior of your program. Inheritance, which is described next, does not have this flexibility since the compiler must place compile-time restrictions on classes created with inheritance.

So you could maintain a person class with a list of attributes like occupation and hobbies/sports.

Community
  • 1
  • 1
Jon Sampson
  • 1,473
  • 1
  • 21
  • 31
  • Exactly. Definitely the better choice considering the example with person/student/footballplayer – Jan B. Jan 29 '17 at 22:00