Total Pageviews

Sunday, January 23, 2011

Inheritance and Accessibility


determine which of the print statement has an error if the code is executed
as new B( ).printInfo from main method ---->


class A
{
int x;
private boolean y;
protected String z;
private static int m;
A( )
{
x = 10;
y = true;
z = "mohit";
m++;
}
public int getX( ){return x};
private void getY( ){return y};
public static void incr( )
{
sop( m );
}
}


class B extends A
{
public void printInfo() {
sop( x ); // 1
sop( y); // 2
sop(getX( )); // 3
sop(getY()); // 4
sop(m); // 5
incr();
}

}

1 comment:

  1. only protected member z will be accessible in child class. So program will give error on compile time.

    ReplyDelete