1

I'm writing a program in Java (11 I think).

I have an abstract class:

public abstract class Homework
{
    // instance variables - replace the example below with your own
    private int pages;
    private String type;

    /**
     * Constructor for objects of class Homework
     */
    public Homework()
    {
        // initialise instance variables
        pages = 0;
        type = "none";
    }

    public void setPages(int pages)
    {
        this.pages = pages;
    }
    public int getPages()
    {
        return pages;
    }
    
    public abstract void createAssignment(int pages);
}

And a subclass:

public class Trigonometry extends Homework
{
    /**
     * Constructor for objects of class Trigonometry
     */
    public Trigonometry()
    {
        super();
    }
    
    public void createAssignment(int pages)
    {
        this.pages = pages;
        type = "Trigonometry";
    }
}

I get an error at this.pages and type in the createAssignment() method.

pages has private access in Homework

type has private access in Homework

Shouldn't Trigonometry inherit pages and type from its parent class? What can I do to fix this?

Community
  • 1
  • 1
  • They are private... – Wulf Mar 11 '19 at 13:04
  • it does inherit them, but it does not have access to them because they are `private` – Svetlin Zarev Mar 11 '19 at 13:04
  • It has to do with **visibility of methods and variables** in java. Just do a google search and you will find plenty of articles. It differs slightly from language to language, but the concepts usually apply to all. – togise Mar 11 '19 at 13:09
  • Because they are... Private instance variables. In order to make them accessible in any subclass, make them protected. – Simeon Ikudabo Mar 11 '19 at 14:21

4 Answers4

3

private members cannot be accessed by the sub-class.

Either the sub-class should pass initial values for these members from its constructor to the super-class constructor, or you can use the public getter and setter methods to access and modify the members from the sub-class.

public void createAssignment(int pages)
{
    setPages(pages);
    setType("Trigonometry");
}
Eran
  • 387,369
  • 54
  • 702
  • 768
2

Private means that something is only accessible inside the class and nowhere else, even in subclasses. The behaviour you're expecting can be achieved by using the "protected" keyword instead of "private". As in

protected int pages; 

instead of

private int pages;

I think this other question about public private and protected is going to be of help.

Answer by David S. and Drew:

The official tutorial may be of some use to you.

            │ Class │ Package │ Subclass │ Subclass │ World
            │       │         │(same pkg)│(diff pkg)│ 
────────────┼───────┼─────────┼──────────┼──────────┼────────
public      │   +   │    +    │    +     │     +    │   +     
────────────┼───────┼─────────┼──────────┼──────────┼────────
protected   │   +   │    +    │    +     │     +    │         
────────────┼───────┼─────────┼──────────┼──────────┼────────
no modifier │   +   │    +    │    +     │          │    
────────────┼───────┼─────────┼──────────┼──────────┼────────
private     │   +   │         │          │          │    

 + : accessible         blank : not accessible
Facundo Uribe
  • 36
  • 1
  • 3
1

In your case, it seems that you want it to be protected instead of private.

More details here : What is the difference between public, protected, package-private and private in Java?

Cyril
  • 254
  • 1
  • 6
1

Because private instance variables are only accessible within the class they're created. If you create a subclass, you'll need to utilize protected instances variables. So you'll either have to make your variables public or preferably protected.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27