Inheritance – Member Access

If any of the method or data member is declared private then that private variable or member method is not inherited to the derived class. Consider an example of this :

class BasicClass {
	int a = 100; // by default package access
	private int j = 200;

	void functionBase() // by default package access, will be inherited
		System.out.println("inside BasicClass");

	private void functionBase2() // this function will not be inherited in
									// derived class
		System.out.println("inside provate function");

	public void functionBase3() // will be inherited
		System.out.println("inside functionbase3");

public class PrivateInheritance extends BasicClass {
	public static void main(String[] args) {
		BasicClass b = new BasicClass();
		System.out.println(b.a);// a is visible here, because of package access
		b.j; // gives error, j not visible here
		b.functionBase(); // will execute as functionBase() available here
		b.functionBase2(); // will give error
		b.functionBase3(); // will execute

You may also like...