[325] 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 (Jonathan Payne)
Wed Jun 14 21:43:40 1995

Date: Wed, 14 Jun 1995 18:11:28 -0700
From: jpayne@starwave.com (Jonathan Payne)
To: Steve.Drach@Eng.Sun.COM
Cc: java-interest@java.Eng.Sun.COM
In-Reply-To: <Roam.1.1.803177251.16838.drach@jadeite> (message from Steve Drach on Wed, 14 Jun 1995 17:47:31 -0800 (PDT))
Reply-To: jpayne@starwave.com

I think you meant to say, "since ClassA.printname is not a VIRTUAL
method".

And you're wrong.  All methods in Java are virtual.  In other words,
Java is an object-oriented programming language by default, unlike
C++.

> Date: Wed, 14 Jun 1995 17:47:31 -0800 (PDT)
> From: Steve Drach <Steve.Drach@Eng.Sun.COM>
> Reply-To: Steve Drach <Steve.Drach@Eng.Sun.COM>
> Mime-Version: 1.0
> Sender: owner-java-interest@java.sun.com
> Precedence: bulk
> X-Info: To unsubscribe, send 'unsubscribe' to java-interest-request@java.sun.com
> Content-Type: TEXT/PLAIN; charset=US-ASCII
> Content-Length: 847
> 
> I'm a little confused about the output from the following program.  It
> seems to me that since ClassA.printname is not an abstract method,
> then it should be called by the a.printname() statement in the main
> method.  Instead the printname method in ClassB is called.  I really
> don't understand why.  The same program in C++ seems to work the way I
> expect.
> 
> class ClassA {
> 	String name = "ClassA";
> 	void printname() {
> 		System.out.println("A: " + name);
> 	}
> }
> 
> class ClassB extends ClassA {
> 	String name = "ClassB";
> 	void printname() {
> 		System.out.println("B: " + name);
> 	}
> }
> 
> class AccessTest {
> 	public static void main(String argv[]) {
> 		ClassB b = new ClassB();
> 		ClassA a = b;
> 		a.printname();
> 		System.out.println(a.name);
> 	}
> }
> 
> 
> -
> Note to Sun employees: this is an EXTERNAL mailing list!
> Info: send 'help' to java-interest-request@java.sun.com
> 

-
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