3

I create an method local Inner Class and combine with abstract class. The code work fine but I do not understand the error popup in IntelliJ about I can't set Method in inner class that extend from abstract inner class to be private.

I have to change from "Private InnerClassSubclass" to "Public InnerClassSubclass" and if I won't the error is follow:

'innerMethod()' in 'InnerClassSubclass' clashes with 'innerMethod()' in 'InnerClass'; attempting to assign weaker access privileges ('private'); was 'public'.

I thought private is stronger privilege isn't it? only allow class within the same class to access.

I also try to change 'abstract class InnerClass' to 'private abstract class InnerClass' also got this error;

"Modifier 'private' not allowed here" at private of 'private abstract class InnerClass'

the code is below:

    public class Outerclass {

    // instance method of the outer class
    private void outer_Method() {
        int num = 23;

        // method-local inner class
        abstract class InnerClass {
            abstract public void innerMethod();
        } // end of inner class

        class InnerClassSubclass extends InnerClass {
            public void innerMethod() { //if I extends, I can't use private for innerMethod here.
                System.out.println("This is method inner class " + num);
            }
        }
        // Accessing the inner class
        new InnerClassSubclass().innerMethod();
    }

    public static void main(String args[]) {
        Outerclass outer = new Outerclass();
        outer.outer_Method();
        }
    }

Could someone clarify me why? Thank you.

Enea Dume
  • 3,014
  • 3
  • 21
  • 36
Smith Lo
  • 305
  • 3
  • 12

3 Answers3

1

'innerMethod()' in 'InnerClassSubclass' clashes with 'innerMethod()' in 'InnerClass'; attempting to assign weaker access privileges ('private'); was 'public'.

This is correct. You will always be able to write

InnerClass ic = new InnerClassSubclass();
ic.innerMethod(); // method is public

Consider this more general case.

static void callInnerMethod(InnerClass ic) {
    ic.innerMethod(); // method is public
}

You can't make this cause a compile error when you pass an InnerClassSubclass. In a more general case you only know the actual type at runtime so it's not solvable at compile time.

InnerClass ic = Class.forName(someString).asSubClass(InnerClass.class).newInstance();

ic.innerMethod(); // this will compile as the method is public.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

You can't restrict the visibility of methods in a subclass.

Assume an Animal class has a public method "breath()". Client code receives an Animal object and invokes that method.

Now imagine you had a Dog subclass, and you pass a doggy object. How should the client code know that his specific Animal does not offer that method?!

Thus: restricting visibility of methods is conceptually wrong, and therefore the compiler gives you an error that exactly says so.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • So, the subclass method must be public only. I will remember this thank you. – Smith Lo Jul 30 '18 at 10:16
  • No. The subclass method visibility can't be **less*. If the base method is protected, you could turn it into public for example. – GhostCat Jul 30 '18 at 10:20
0

Firstly, you cannot make the method private because as per java overriding principles, child class cannot make the access modifier of overriding method tighter than that in it's parent class. Here, because parent class have access modifier as public, it should be made public in child class as well

Secondly, inner class can only have only two access modifier : abstract and final. You cannot declare it as private

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
  • can you recommend a book or any links about Java background that I can improve my knowledge? I feel my fundamental knowledge about Java is pretty weak :( Thanks. – Smith Lo Jul 30 '18 at 10:18
  • I will tell you what I read to this. JAVA The Complete Reference by Herbert Schildt. For advance go for OCP Java SE 8 Programmer by Kathy Sierra – Ashishkumar Singh Jul 30 '18 at 10:27