Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How private method of super class are resolved?

Tags:

java

class A{ 
  private void sayA(){
     System.out.println("Private method of A");
  }
  public static void main(String args[]){
      A instanceA=new B();
      instanceA.sayA();
  }
}

class B extends A{
}

I was expecting it to throw a run time exception as at compile-time the compiler checks if sayA() can be called on a reference of A and at run-time it'll check if sayA() can be called on B's object. But it instead printed "Private method of A".

like image 362
Pankaj Avatar asked Jun 03 '15 01:06

Pankaj


People also ask

Can a subclass of a class have private methods?

Yes, subclass can have private method same as in superclass but it can not declare it as overridden method as superclass method will not be visible after being declare as private. Yes,we can override with public methods for private methods of base class,but we can’t with private methods in public methods of super class.

Is the print () method inherited from the Super class?

Here as the print () method is declared private in the super class privateImp, and we extend that class. When I create a subclass object and try to access the inherited method access () it is also invoking the private method of the super class.

What is overriding superclass methods in Java?

Overriding Super Class Methods in Java | Inheritance in Java 9.3 Overriding Superclass Methods In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

Which class is calling the private method at run time?

Access control in Java is not based on which class is calling the method at run time. It's based on where the calling code is in the source code. Here, the call to the private method is in the privateImp class, so it's allowed.


2 Answers

Accessibility is a compile time concept (reflected in Reflection APIs).

From the Java Language Specification

Note that accessibility is a static property that can be determined at compile time; it depends only on types and declaration modifiers.

That is, the compiler doesn't care what the runtime type of the instance referenced by your variable named instanceA will be

A instanceA = new B();

It only cares that you invoked a method on a reference of static type A. That method is private and since you are within the body of the class which declares it, it is visible, and therefore usable.

Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.


For spiderman in comments, consider the following

class A {
    private void privateMethod () {
        System.out.println("private method");
    }
    public void publicMethod() {
        privateMethod();
    }
}

class B extends A {}

class Example {
    public static void main(String[] args) {
        new B().publicMethod();
    }
}
like image 177
Sotirios Delimanolis Avatar answered Sep 26 '22 01:09

Sotirios Delimanolis


private means that only the class that declares the field can see it. Because you're calling instanceA.sayA(); from within class A the method is visible and the code both compiles and runs. If you were to try to call that method from within class B or any other class you would get the compile warning that The method sayA() from the type A is not visible

like image 28
1337joe Avatar answered Sep 26 '22 01:09

1337joe