3

I am writing small pieces of code to make sure I understand Java basics and I have the following.

package teams1;

public abstract class Team1{
    private String sport = new String();

    public abstract String getSport();

    public abstract void setSport();
}

import teams1.*;
abstract class FootballTeam1 extends Team1{

    public String getSport(){
        return "Football";
    }

    public void setSport(){
        this.sport="Football";
    }
}

It doesn't compile because sport is private in the super class, but I thought FootballTeam1 would inherit it's own copy of sport because it is extending Team1. Any help would be appreciated. Thanks!

2
  • 5
    Private means it's not available to child classes (or anything external for that matter). If you want it to be available to child classes it will have to be protected. (Incidently the FootBallTeam1 still has a sport field, it just can't access it directly) Commented Nov 19, 2014 at 13:21
  • And this has nothing to do with the class being abstract. Commented Nov 19, 2014 at 13:23

5 Answers 5

5

You have mostly answered your own question. FootballTeam1 does not have access to the private fields of its parent. That is what the 'protected' scope is used for.

However, the child FootballTeam1 does have its own copy of the field. It has a copy of all fields that the parent class has, which I can see would cause confusion.

The reason for this distinction is modularity. A subclass of a parent class only has access to the parts of the parent class that one has explicitly stated that it may have access to. This allows developers to consider what parts of a class are to be exposed, under the Object Orientated goal known as the 'Open/Closed Principle'; that is, classes should be open for extension, but closed for modification.

The quickest 'fix' to the class is change the scope of the field, for example

private String sport = new String();

becomes

protected String sport = new String();

or

public String sport = new String();

If you do not want to give the child class direct access to the field, but do want to allow it to change the field then a protected setter method could be used. For example, you could add the following to Team1.

protected void setSport( String newValue ) {
    this.sport = newValue;
}
Sign up to request clarification or add additional context in comments.

2 Comments

How would the child FootballTeam1 access its own copy of the field?
@ponder275 see the first sentence, protected is a field or method keyword that is used to give visibility to classes that extend the class but not those outside of the inheritance tree. I will tweak the answer to make that clearer.
2

Since the class variable sport is private, it is private to the class it was declared in. Extending classes cannot access this variable in the manner you are trying.

Try making the variable protected (which allows extending classes to have visibility on the variable) if you want to continue accessing the sport variable in this manner, otherwise have getters and setters in the abstract class and the extending/implementing classes to call them instead.

Comments

2

Private Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

Protected Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

Modified code :

package com.org.abstractc;
public abstract class Team1{
    // you have to change this private to protected then it will be inherited 
    // in child class.
    protected String sport = new String();   

    public abstract String getSport();

    public abstract void setSport();
}

2 Comments

So I either need to use protected or make the getSport & setSport methods concrete and override them if necessary?
Yes! if you are implementing your methods in subclass (FootballTeam1), you have to change access modifier of super class (Team1) variable (sport), because private members are not inherited in subclass. so, you have to change private to protected and if you don't want to change the access modifier you have to implement your method inside abstract class(Team1) itself to access sport variable.
0

Just change private to protected. private means that your subclasses don't have access to variables or methods, whereas protected allows this access.

Comments

0

Private fields are accessible only in the same class. Also, inheritance is mainly used for defining the same name methods in derived classes with seperate functional logic.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.