Results 1 to 5 of 5
  1. #1
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default using inheritance

    hey,
    I'm creating some sort of soccergame in java. I want to use inheritance to make it easier but I don't really know what I need to put in my subclasses to inherit. I've made a class player. Fieldplayer and Goalkeeper need to inherit from it. I need to put setters in my subclasses if I want to set something but setting should be working automatically when inheriting? Don't know if I've written to much code in my subclasses :confused: . These are the classes:

    player:
    Java Code:
    import java.util.*;
    
    public class Player{
        private String name = "Unknown";
        private String surname = "Unknown";
        private Datum dateOfBirth = new Datum();
        
        
        //constructor//
    public Player(){}
    public Player(String name, String surname, Datum dateOfBirth){
        this.setName(name);
        this.setSurname(surname);
        this.setDateOfBirth(dateOfBirth);
        }
        
    //getters//
    public String getName(){
        return this.name;}
    public String getSurname(){
        return this.surname;}
    public Datum getDateOfBirth(){
        return this.dateOfBirth;}
    
        
    //setters//
    protected void setName(String name){
        if(name != null && !name.equals(""))
            this.name=name;}
    protected void setSurname(String surname){
        if(surname != null && !surname.equals(""))
            this.surname=surname;}
    protected void setDateOfBirth(Datum dateOfBirth){
        if(dateOfBirth != null)
            this.dateOfBirth=dateOfBirth;}
    
       
    }
    fieldplayer:

    Java Code:
    public class Fieldplayer extends Player{
        //attributes//
        private int speed = 1;
        private int stamina = 1;
        private int acceleration = 1;
        private int strength = 1;
        private int shotpower = 1;
        private int passing = 1;
        private int accuracy = 1;
        private int finishing = 1;
        
    
        
        
    public Fieldplayer(int speed, int stamina, int acceleration, int strength, int shotpower, int passing, int accuracy, int finishing){
        this.setStrength(strength);
        this.setShotpower(shotpower);
        this.setPassing(passing);
        this.setAccuracy(accuracy);
        this.setFinishing(finishing);
        }
        
    //getters//
    public int GetSpeed(){
        return this.speed;}
    public int GetStamina(){
        return this.stamina;}
    public int getAcceleration(){
        return this.acceleration;}
    public int getStrength(){
        return this.strength;}
    public int getShotpower(){
        return this.shotpower;}
    public int getPassing(){
        return this.passing;}
    public int getAccuracy(){
        return this.accuracy;}
    public int getFinishing(){
        return this.finishing;}
        
    //setters//
    
    private void setSpeed(int speed){
        if(speed > 0 && speed <= 30)
            this.speed=speed;}
    private void setStamina(int stamina){
        if(stamina > 0 && stamina <= 30)
            this.stamina=stamina;}
    private void setAcceleration(int acceleration){
        if(acceleration > 0 && acceleration <= 30)
            this.acceleration=acceleration;}
    private void setStrength(int strength){
        if(strength > 0 && strength <= 30)
            this.strength=strength;}
    private void setShotpower(int shotpower){
        if(shotpower > 0 && shotpower <= 30)
            this.shotpower=shotpower;}
    private void setPassing(int passing){ 
        if(passing > 0 && passing <= 30)  
            this.passing=passing;}
    private void setAccuracy(int accuracy){ 
        if(accuracy > 0 && accuracy <= 30)
            this.accuracy=accuracy;}
    private void setFinishing(int finishing){
        if(finishing > 0 && finishing <= 30)
            this.finishing=finishing;}
    }
    goalkeeper:
    Java Code:
    import java.util.*;
    
    public class Goalkeeper extends Player{
        //attributes for goalkeepers//
        private int reactionspeed = 1;
        private int grip = 1;
        private int diving = 1;
        
    public Goalkeeper(int reactionspeed, int grip, int diving){
        this.setReactionspeed(reactionspeed);
        this.setGrip(grip);
        this.setDiving(diving);}
        
    public int getReactionspeed(){
        return this.reactionspeed;}
    public int getGrip(){
        return this.grip;}
    public int getDiving(){
        return this.diving;}
        
    
    public void setReactionspeed(int reactionspeed){
        this.reactionspeed=reactionspeed;}
    public void setGrip(int grip){
        this.grip=grip;}
    public void setDiving(int diving){
        this.diving=diving;}
                
      
    
    
    }

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    In the subclasses constructor, you should have this as your first statement:
    Java Code:
    public Constructor(..) {
         super();
    }
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your subclasses will inherit all non-private methods and fields from the parent class including its public getters and setters. In fact, you should probably change your subclass getters and setters to public so that they can be used outside of the class (that's their purpose).

    Also, you'll probably want to change the subclass constructors so that their first three parameters are String, String and Datum (whatever that is), for name, surname and dateOfBirth, and then pass these parameters to the super constructor in the first line of the child constructor.

    i.e.,
    Java Code:
    public Fieldplayer(String name, String surname, Datum dateOfBirth, int speed, int stamina, int acceleration, int strength, int shotpower, int passing, int accuracy, int finishing){
        super(name, surname, dateOfBirth);
        this.setStrength(strength);
        this.setShotpower(shotpower);
        this.setPassing(passing);
        this.setAccuracy(accuracy);
        this.setFinishing(finishing);
    }
    Last edited by Fubarable; 08-15-2010 at 07:39 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    In the subclasses constructor, you should have this as your first statement:
    Java Code:
    public Constructor(..) {
         super();
    }
    The default super constructor is not explicitly needed as it is implicitly present if no other super constructor is explicitly called. My opinion is that you should use the constructor changes that I have suggested above. You also may consider giving the subclasses default parameterless constructors. Again these will implicitly call the super's default parameterless constructor without need for the explicit call shown above.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    I want to use inheritance to make it easy.
    Inheritance is highly overrated feature in object oriented programming. If, in your example, getting/setting names and ages is all there is in the superclass, well that's all there is. One of the pitfalls of inheritance is making a superclass 'too strong' for the extending class. The superclass may have behaviour you don't want in your subclass but can't get rid of properly.

    Leave your classes as they are: a boring superclass and subclasses that implement most of the behaviour. This'll be a small hierarchy, but if it works, it works. Don't expect miracles from inheritance.

    kind regards,

    Jos

Similar Threads

  1. Inheritance
    By Nerijus in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 03:47 AM
  2. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 10:41 AM
  3. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 02:53 PM
  4. inheritance
    By itaipee in forum New To Java
    Replies: 6
    Last Post: 01-20-2009, 08:18 PM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •