why don't we can't make a overridden method less accesible

why don't we can't make a overridden method less accesible. i know if i do this the code won't be compile............but i want to know the reason........

Assume if compiler allow the code to compile...wat happens at run time for following code...........

class Animal

{

public eat()

{

System.out.println("m in Animal class");

}

}

class horse extends Animal

{

protected eat()

{

System.out.println("m in Hourse class");

}

}

public class Example{

public static void main(Srting arg[])

{

Animal a = new Hourse();

a.eat();

}

}

Assume Animal and Horse classes are inthe same package

[732 byte] By [Mayanknaagara] at [2007-11-26 18:31:47]
# 1

Think of a class definition as a contract. The "Animal' contract implies that if you have an object which is an Animal or a subclass of Animal you are declaring that any code (regardless of whether it is in the same package or is a subclass) can call the eat method on a visible instance of that object. The horse class breaks that contract. A piece of code that is given an object that inherits from the horse class may not be able to call the eat method.

Even though the Animal and horse classes are only visible to the package, you can still create a public subclass of horse which must then, by the terms of the Animal contract, have a public eat method. The eat method on the horse class would break that contract.

Hope that makes sense.

Graeme

GraemeHooka at 2007-7-9 6:05:55 > top of Java-index,Java Essentials,Java Programming...
# 2
Thnax ................... GraemeHook
Mayanknaagara at 2007-7-9 6:05:55 > top of Java-index,Java Essentials,Java Programming...