Private members are not inherited? That constructor do?

Firstly, the conclusion that private members are inherited, but can not be called explicitly, constructors are not inherited. The problem is that I brush past few days at a site above the title encounter, I feel more interesting. Let's verify. How to verify it, by reflection!

Directly create two classes, the parent class A, subclass B:

class A {
private void prA() {
System.out.println("父类私有");
}
}

class B extends A {
}

Then the next test:

    public  static  void main (String [] args) throws a ClassNotFoundException, a InvocationTargetException, IllegalAccessException { 

        // obtained by subclasses of the parent class array 
        Class bClass the Class.forName = ( "B" ); 
        Method, [] aMethods = bClass.getSuperclass () .getDeclaredMethods ();
         // set method may be private access 
        AccessibleObject.setAccessible (aMethods, to true );
         for ( int I = 0; I <aMethods.length; I ++ ) { 
            aMethods [I] .invoke ( new new B (), null ); 
        } 

        Class aClass= bClass.getSuperclass();
    }

Results: The parent class private

The last quoted at official word on the constructor:

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. [Therefrom subclass parent class inherits all the members (fields, methods and nested classes). Constructors are not members, they are not inherited by subclasses, but you can call the superclass constructor from a subclass. ]

 

Guess you like

Origin www.cnblogs.com/junalncer/p/11031869.html