Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced object casting problem, static methods/variables?

Status
Not open for further replies.

bigDaftie

Programmer
Feb 28, 2003
36
GB
I'm looking at some past papers for an exam and have a problem with the following answers to a question (answers are beside output in the main method) that you have to give the output for. I think that when you upcast in this manner A a=(A)b if you call a.aMethod, if the method appears in both classes but is static in the above class, it will use that method. It will access the variables from the class above, with a.aVariable, but will you use the methods in the original class (and the variables in the original class if called from the method) if the method is not static.
Code:
public class Q1a {

  public Q1a() {
  }
  public static void main(String[] args) {
    B b =new B();
    System.out.println("i)"+b.i);//answer is 2
    System.out.println("ii)"+b.f());//B
    System.out.println("iii)"+b.g());//4

    A a=(A)b;

    System.out.println("iv)"+a.i);//1
    System.out.println("v"+a.f());//A
    System.out.println("vi)"+a.g());//4
  }
}

class A{
    static char f(){
      return 'A';
    }
    int i=1;
    int g(){
      return i;
    }
  }
  class B extends A{
    static char f(){
      return 'B';
    }
    int i=2;
    int g(){
      return 2*i;
    }
  }
I've looked on the Web, in books and searched the forum but can't seem to find anything advanced enough that explains this in any way. If anyone has any knowledge of this or any URL's to anything I would be very grateful, as I need to read up on this subject. I want to understand this fully and what is happening in memory as well if that's possible.
Thanks a lot.
 
I have just taken the SCP exam, and this web site:-


Is very useful in understanding these issues. Section 6 - inheritance, addresses the issues you mention, and the answers explain the reasoning. I was always confused about these differences b4 I read "carefully" the explanations.

BTW, if anyone is considering taking the Sun exam, the above web site is a very good (if a bit tough) selection of questions. I went through all the Q&A, and examined in detail why I got questions wrong to make sure I fully understood the reason (it took about 20 hours in all). I then went to other web sites to answer their mock exams and found these to be much easier. The real exam was therefore a piece of cake.
 
Static methods are not virtual and therefore do not employ polymorphism. Ok I guess that probably doesn’t help.

A static method is like a global function, it is not bound to any instance of the class and only has access to static data since there is no instance there can be no instance data. Therefore whichever static method you call, that’s the one that gets executed despite any super/sub class relationships. In effect you don’t override the static method you “replace or hide” it via scope.

-pete
 
This is GREAT, thanks a lot. Appreciate your help. It's all starting to become much clearer.
Bruce
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top