Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-09-2007, 03:01 PM
Member
 
Join Date: Aug 2007
Posts: 9
rhobincu is on a distinguished road
Invoking superclass methods... how?
Hi,

I'm not really new to Java but I'm posting here since the problem appears to simple for the advanced forum. I've written these 2 test classes:

Code:
public class A{ public void doS(){ System.out.println("A.doS()"); } }
and

Code:
public class B extends A{ public void doS(){ System.out.println("B.doS()"); } public static void main(String args[]){ B b = new B(); b.doS(); ((A)b).doS(); } }
Now, I was under the impression that since I casted the "b" object to class "A", invoking the "doS()" method will call the corresponding method from class "A". However the result is:

Code:
[rhobincu@st27 stuff]$ java B B.doS() B.doS()
Disassembling the code made it even weirder:

Code:
[rhobincu@st27 stuff]$ javap -c B Compiled from "B.java" public class B extends A{ public B(); Code: 0: aload_0 1: invokespecial #1; //Method A."<init>":()V 4: return public void doS(); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String B.doS() 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return public static void main(java.lang.String[]); Code: 0: new #5; //class B 3: dup 4: invokespecial #6; //Method "<init>":()V 7: astore_1 8: aload_1 9: invokevirtual #7; //Method doS:()V 12: aload_1 13: invokevirtual #8; //Method A.doS:()V 16: return }
I'm noticing that the second invokevirtual instruction really calls A.doS()V with no apparent success. I also notice no "checkcast" instruction.

Is there a problem with my JDK? Or am I missing smth?

Appreciating any suggestions,
Radu
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-09-2007, 03:29 PM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
Polimorfism
Hi,
The output of your code is ok.
When u invocke a method on an object, at runtime the virual machine is able to know the specific class to which your object belongs.
In your example code, u create an object which belongs to the class B, even if u cast it, when u call a method that u have overwritten, at runtime will be executed the method of the specific class of the object( the is the method in the class B).
Bye.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-09-2007, 04:00 PM
Member
 
Join Date: Aug 2007
Posts: 9
rhobincu is on a distinguished road
Ok, so the only way to invoke an overwritten superclass method is to write a method in your extended class that does "super.doS()"?

What if I have:

java.lang.Object ---> testClass.A ---> testClass.B ---> myTestClasses.C

If all A, B and C classes have a method called "doS" there is no way to access testClass.A.doS() from myTestClasses.C considering I only have the compiled code (.class / .jar files) for classes A and B?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-09-2007, 04:12 PM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
Yes, there will be no way...if u really need it u can just not overwrite it.
U dont have the source code????
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-09-2007, 04:30 PM
Member
 
Join Date: Aug 2007
Posts: 9
rhobincu is on a distinguished road
It's not that I don't have the source code, I'm just trying to figure out EXACTLY how JVM works becouse we're working at a Java hardware machine and we need to get it right.

At the first sight it seemed normal to be able to do what I wrote in the first post but it seems that I was wrong. I'm wondering how many surprises I'm up for.

Thanks for your help tho,
Radu
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-09-2007, 04:50 PM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
Regarding polimorfism,
U can have many superclasses as u like till u arrive to the Object class that is the superclass of all classes. There is no limit to the superclsses.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-09-2007, 04:59 PM
Member
 
Join Date: Aug 2007
Posts: 9
rhobincu is on a distinguished road
Yes, I understand that, it was the issue of accessing the public & protected fields & methods in those superclasses that bugged me. Especially if some of those classes overwrite methods.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-09-2007, 05:10 PM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
Hope u got things clear now..if u dont u can just ask whatever u want,
Bye.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Invoking a Stateful Session Bean as Stateless JThangiah Enterprise JavaBeans 3 07-17-2008 11:35 AM
Extract superclass(I) JavaForums Java Blogs 0 03-12-2008 12:40 AM
Getting name of superclass (Example: ArithmeticException) Java Tip Java Tips 0 11-12-2007 01:53 PM
invoking method and output questions bluekswing New To Java 1 08-06-2007 08:36 AM
mapping servlets in user defined xml file and invoking them praneeth Advanced Java 0 07-16-2007 10:45 PM


All times are GMT +3. The time now is 11:12 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org