Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic