Results 1 to 8 of 8
  1. #1
    rhobincu is offline Member
    Join Date
    Aug 2007
    Posts
    9
    Rep Power
    0

    Default 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:

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

    Java 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:

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

    Java 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

  2. #2
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default 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.

  3. #3
    rhobincu is offline Member
    Join Date
    Aug 2007
    Posts
    9
    Rep Power
    0

    Default

    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?

  4. #4
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default

    Yes, there will be no way...if u really need it u can just not overwrite it.
    U dont have the source code????

  5. #5
    rhobincu is offline Member
    Join Date
    Aug 2007
    Posts
    9
    Rep Power
    0

    Default

    :) 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

  6. #6
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default

    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.

  7. #7
    rhobincu is offline Member
    Join Date
    Aug 2007
    Posts
    9
    Rep Power
    0

    Default

    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.

  8. #8
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default

    Hope u got things clear now..if u dont u can just ask whatever u want,
    Bye.

Similar Threads

  1. [SOLVED] Invoking a Stateful Session Bean as Stateless
    By JThangiah in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 07-17-2008, 10:35 AM
  2. Replies: 0
    Last Post: 11-12-2007, 12:53 PM
  3. invoking method and output questions
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:36 AM
  4. Replies: 0
    Last Post: 07-16-2007, 09:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •