Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
O. Subramaniam
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chip Gobs
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chip,
you are right, i shouldn't drink so much Sorry about that.
 
O. Subramaniam
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u r right CHIP, this has nothing to do with inheritance. SORRY
 
Joe Pluta
Ranch Hand
Posts: 1376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic