Java programming question?

Discussion in 'Mac Programming' started by AuntJemima, Apr 30, 2009.

  1. AuntJemima macrumors newbie

    Joined:
    Jan 14, 2008
    #1
    Sorry I m studying java and alittle new to the language but have a question about inheritance....

    if I have a bunch of classes and each on extends the next and I add an overrided method to the 'class2' after the 'class1', will the method from 'class1' that has been override by the 'class2' be forgotten because it has been overridden or what?

    I know this happens with constructors but what about my situation?

    Thanks
    jgwc
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    It's not "forgotten" but the overriding method will be called. You can still use super.methodName from class2 to call the method in class1...
     
  3. AuntJemima thread starter macrumors newbie

    Joined:
    Jan 14, 2008
    #3
    but if I don't call it(i.e. super.methodName) than it won't be automatically included in the extended class('class2')?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    No it won't be called automatically.
     
  5. AuntJemima thread starter macrumors newbie

    Joined:
    Jan 14, 2008
  6. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
  7. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #7
    But what happens if you... (aside from the fact that this probably won't compile but hopefully you get the drift...)

    Code:
    class MyClassA {
        public void myMethod() {
            System.out.println("Class A");
        }
    }
    
    class MyClassB extends MyClassA {
        public void myMethod() {
            System.out.println("Class B");
        }    
    }
    
    MyClassA myObject = new MyClassB();
    myObject.myMethod();
    
    
    Does it print "Class A" or "Class B"? I've been reading too much (or perhaps not enough if the code is bad enough) of my Java Cert book...
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    I've not actually bothered checking, but I believe it should print "Class B"...
     
  9. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #9
    Hi

    It will print Class B... it's an example of polymorphism, all methods in Java are polymorphic... (I think!).

    ß e n
     
  10. AuntJemima thread starter macrumors newbie

    Joined:
    Jan 14, 2008
    #10

    I don't know if you were referring the question to me but I am using a book called 'Beginning Java Objects From Concepts to Code'

    Is that what you were looking for?
     
  11. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #11
    Should be "Class B", while the reference (containter) is pointing to A, the actual object your are invoking the method on is an instance of B.

    Are you studying to get a SCJP cert?

    yeah you :) Take a look at Head First Java, I think it is a must to learn the language. Oh, and it is a blast to use too.
     
  12. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #12
    Yeah. I was mostly posing it to the forum as I had recently read the answer. I've been doing Java coding for seemingly ever so most of the book is just a rehash of things I already do day to day.
     

Share This Page