[341] in java-interest

home help back first fref pref prev next nref lref last post

Re: confusion over which method is invoked

daemon@ATHENA.MIT.EDU (Andrew Wason)
Thu Jun 15 13:54:14 1995

To: java-interest@java.Eng.Sun.COM
In-Reply-To: Message from David.Geary@central.sun.com (David Geary) 
   of "Thu, 15 Jun 95 09:56:52 +0700."   <9506151556.AA02072@trilby.Central.Sun.COM> 
Date: Thu, 15 Jun 95 13:34:08 -0400
From: Andrew Wason <aw@bae.bellcore.com>

David.Geary@central.sun.com (David Geary)  writes:
> > > class AccessTest {
> > > 	public static void main(String argv[]) {
> > > 		ClassB b = new ClassB();
> > > 		ClassA a = b;
> > > 		a.printname();
> > 
> > Because printname is a virtual method, it will actually
> > call the printname in ClassB.
> 
>   In C++, it makes no difference whether printname() is virtual or not.
> a.printname() should invoke A::printname(), since a is an object of class A.

In Java the variables 'a' and 'b' are really references, so 'a =
b' just makes 'a' reference the object referenced by 'b'.  And
since printname() is virtual, it's invoked in ClassB.

In C++ the above code might look like this (using pointers):

   ClassB *b = new ClassB();
   ClassA *a = b;
   a->printname();

and so ClassB::printname() is invoked.


Andrew

_______________________________________________________________________________

Andrew Wason                                       Bell Communications Research
aw@bae.bellcore.com                                Piscataway, NJ
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post