How inheritance works with abstract classes in java

I am writing small snippets 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 the sport is private in the superclass, but I thought FootballTeam1 inherits its own copy of the sport because it extends Team1. Any help would be greatly appreciated. Thank!

+3


source to share


5 answers


You basically answered your own question. FootballTeam1 does not have access to its parent's private fields. The protected area is used for this .

However, the FootballTeam1 child has their own copy of the pitch. It has a copy of all the fields the parent class has, which I can see can be confusing.

The reason for this difference is modularity. A subclass of the parent class only has access to parts of the parent class that has explicitly indicated that it can have access. This allows developers to consider which parts of the class should be exposed within the Object Orientated target known as the "Public / Private Principle" , that is, classes should be open for extension but closed for modification.

The quickest "fix" for a class is to change the field scope, 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 a field, but want to allow it to modify that field, then a secure set method can be used. For example, you can add the following to Team1

.

protected void setSport( String newValue ) {
    this.sport = newValue;
}

      

+5


source


Since a class variable sport

is private, it is private to the class in which it was declared. Class extension cannot access this variable in the way you are trying.



Try making a variable protected

(which allows classes to be extended for visibility in a variable) if you want to continue accessing the variable sport

that way, otherwise there are getters and setters in the abstract class and extending / implementing classes instead.

+2


source


Private

Private methods, variables, and constructors can only be accessed by the declared class itself.

Protected

Variables, methods, and constructors that are declared protected in a superclass can only be accessed by subclasses of another package or any class in the package of a protected member 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


source


Just change private

to protected

. private

means that your subclasses do not have access to variables or methods, whereas it does protected

.

0


source


Private fields are only available in one class. In addition, inheritance is mainly used to define the same named methods in derived classes with separate functional logic.

0


source







All Articles