Praneeth,
Pls read the notes on inheritance.
We have to learn the behavior as Rules. just apply the rules.
class A
{
int i=20;
}
class B extends A
{
private int i=99;
}
class C extends B
{}
class test99
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
System.out.println(a.i);
//System.out.println(b.i);
System.out.println(c.i);
}
}
1)Since B extends A,it already 'has' int i.So why is it allowed to redeclare it and make it private.
2)Why doesnt c.i compile?Though i is private in B,it has default access in A, and C is sub class of A indirectly.Why doesnt this compile?
[/QB]
it is very simple.
1) redeclaration is allowed in inheritance, now B.i is different from A.i, Programmer can prevent a variable access in sub class by specifying it as private though it is declared in super class.
2) c.i is won't compile. because the immediate super class B is having private access to i, then its subclass cannot access it.
Thanks
Madhu