This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

O. Subramaniam
Greenhorn

Joined: Aug 04, 2003
Posts: 17
class A
{
public void display()
{
System.out.println("A");
}
}
the above program was saved as A.java
class B
{
public static void main(String args[])
{
A a=new A();
a.display();
}
}
this program was saved as B. java.
both the programs were compiled.
now when i run the second program B.java it gives the output "A".
now i change the "A.java" as follows
class A
{
private void display()
{
System.out.println("A");
}
}
after saving the program i compiled it.
now when i run B.java without compiling it again it gives the output "A".
since the display() method is of private scope it should give error!
am i right?
can anyone explain why it is giving the output imstead error.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
why should it cause an error ? you are calling your private method from A not from B.

private means only that only instances of the class can access the method/variable, and no one else.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
book worm,
Welcome to JavaRanch!! Please change your name to be compliant with JavaRanch's naming policy. It should not be obviously fictitious.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy


"JavaRanch, where the deer and the Certified play" - David O'Meara
Chip Gobs
Greenhorn

Joined: Feb 15, 2001
Posts: 13
Oliver,
Actually there should be an error. Private means that B can't access (use one of A's methods or attributes). If you still think you are
correct, please show us an example of what it means to be private.
Book Worm,
I tried your example and it runs as you say (without error) on my machine.
Maybe the spec says the VM doesn't have to check for access rights at runtime, only at compile time? This is a little surprising to me, but
not that big a deal.
BTW, this has nothing to do with inheritance.
Chip
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
i tried that example on windows XP yest nite when saw the post and now i've the same on unix.
on windows XP it works the way we see.
on Unix i get following error,
----------------
Exception in thread "main" java.lang.IncompatibleClassChangeError: A: method display()V was previously not private
at B.main(B.java:4)
----------------
so i guess its all jvm dependent and as there is probably no standard set (or atleast no tightly defined one) about how jvm shd treat this thing, it would be the descretion of the jvm implementer...
regards
maulin
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Chip,
you are right, i shouldn't drink so much Sorry about that.
O. Subramaniam
Greenhorn

Joined: Aug 04, 2003
Posts: 17
u r right CHIP, this has nothing to do with inheritance. SORRY
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
Originally posted by Maulin Vasavada:
it would be the descretion of the jvm implementer...

Personally, I'd think it would not be up to the JVM implementer. To me, this is an error plain and simple. Think of it this way: if you compile a class MyClass that uses MyWidget.aMethod, and then later you change MyWidget to not have that method, you will get a NoSuchMethod when you try to run MyClass.
This is no different - the change in state of the invoked method should be checked when the class is first loaded. I think somehow the JVM in Windows is not checking permissions at runtime. (By the way, it happened the same way on my machine running Windows 2000 and JRE 1.4.1_02 from Sun.)
Joe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance
 
Similar Threads
using package getting compiler error
Not able to COMPILE!!!
Import
Newbie question : package
How main in superclass is called from subclass