• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
{
private eat()
{
System.out.println("m in Hourse class");
}

}

public class Example{
public static void main(Srting arg[])
{
Animal a = new Hourse();
a.eat();
}
}
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Mayank

It works like this:

In Animal, eat() is public. public access means just that, eat() can be called from any portion of your Java program that can execute a method.

the other three access levels restrict access as follows:
1) protected - method is accessible only to classes in the same package as the original class, or to classes that extend it.
2) default access - method is accessible only to other classes in the same package.
3) private - method is only accessible to other methods in the same class.

thus, Horse's eat() method is only accessible to other methods inside horse.
This means that even though your reference type is Animal, the implementing type is actually a Horse with a private eat method. This means that the code



violates the access control rights - because a is actually a Horse object, eat() is not accessible and thus the code is semantically incorrect and thus cannot be compiled.

If you somehow managed to get it to compile, I imagine that you'd get a NoSuchMethodException or some other nasty error at runtime.

This is why the Java language specification allows you to decrease the access control, since doing so will not result in the situation above.

Hope this helps.

Jeremy
 
Mayank Gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnaks Jeremy,

But what would be your answer if I make the eat method proteced in Horse class. Assume Animal and Horse class in in same package.
 
Jeremy Botha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly the same reasoning applies. The compiler would know that you could call Animal.eat() anywhere, but couldn't do the same for Horse.eat(). Regardless of where you called eat(), the compiler recognises that it cannot guarantee that eat() will be accessible everywhere it has to be, and thus won't compile the code.

J
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The more formal reason is the Liskov Substitution Principle: a subtype needs to be usable everywhere the supertype is usable. So every method that can be called by a client on the supertype, the same client also needs to be able to call on a subtype.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic