0

I wonder if a class having the main method can extend an other class. When I tried, there was an error showing

the constructor is not visible

The solution was to change "protected" to "public". However, I know if a class inherits from an other class, it can use protected methods. Here, this didn't work. Does anyone know what's wrong with this?

package second;
import first.Accumulator;

public class Saving extends Accumulator {

    protected Saving() {
        super()
    }

    protected Saving(int num) {
        super(num);
    }

    protected void addAmount(int amount) {
        add(amount);
    }

    protected void showAmount() {
        show();
    }

}

package third;

import second.Saving;

public class SavingTest extends Saving {

    public static void main(String[] args) {

        Saving saving = new Saving(100);
        saving.addAmount(100);
        saving.showAmount();





    }
}

The result: The Constructor Saving(int) is not visible.

Vega
  • 27,856
  • 27
  • 95
  • 103
  • Possible duplicate of [How can ‘protected static’ variable of superclass be accessed in the subclass, where subclass resides in different package..?](https://stackoverflow.com/questions/17267576/how-can-protected-static-variable-of-superclass-be-accessed-in-the-subclass-w) – Joe C Jan 19 '19 at 10:59
  • 1
    It isn't very clear what the problem is – Soutzikevich Jan 19 '19 at 12:46

1 Answers1

0

The point is "protected" modifier allows you to access methods within subclasses or the same package. In your scenario you neither access Saving(int num) constructor in the same package nor you access it from subclass.

Despite the fact you're trying to instantiate Saving in method of it's subclass in this case it is actually attemt to access protected method/constructor from the outside of this class. I will try to modify you example to show you the difference.

package second;
import first.Accumulator;

public class Saving extends Accumulator {
    public Saving() { // change this one to public to have possibility to instantiate it
        super(); 
    }

    protected Saving(int num) {
        super(num);
    }

    protected void addAmount(int amount) {
        add(amount);
    }

    protected void showAmount() {
        show();
    }
}

package third;

import second.Saving;

public class SavingTest extends Saving {

    // this IS the instance of our superclass
    // but we can't access its protected methods 
    // from THIS instance because it is NOT the same instance
    // either we can't access it from static methods
    private Saving saving1 = new Saving(); 

    public static void main(String[] args) {
        Saving saving = new Saving(100);
        saving.addAmount(100);
        saving.showAmount();
    }

    protected Saving(int num) {
        super(num); // still can access protected constructor of superclass here
    }

    public void nonStaticMethod() {
        saving1.addAmount(100); // can't do this, we try to access protected method from another package and NOT from subclass
        addAmount(100); // can do this! we really access protected method of THIS instance from subclass 
    }
}

So, the bottom line is there is no any problem to inherit another class with class containing main method. You have actually done it, but your code doesn't compile because of wrong actions in main method itself.