Can someone explain this behaviour?Is it a bug or I am missing something obvious?
1)Create 2 packages, say pack1 and pack2 in same directory.
2)In pack1 create a class X
package pack1;
import pack2.*;
public class X
{
void eat()
{
System.out.println("X eat");
}
public static void main(String args[])
{
X x = new Y();
x.eat(); //accessing eat() method on Instance of Y.
//But since eat() is not public or protected its visibility must be limited to class X
System.out.println("Done");
}
}
3)Now in pack2 create a class Y
package pack2;
import pack1.*;
public class Y extends X
{
}
The method eat shouldn't be available to class Y as it has 'default' access specifier which limits its visibility to the package it is declared (Package X). So, this method shouldn't be available in class Y. But When I compile and Execute this code, it works fine. Isn't its a violation of 'default' access specifier?
Also If I change X x = new Y() to Y x = new Y(), then compilation fails!!