Results 1 to 16 of 16
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Superclass Question

    So I'm going over Superclasses and they seem simple enough, but the book does a terrible job explaining them. Not only that but looking through google has given me the same answer as in my book. Just worded slightly different.

    My question is is a class only considered a superclass when it has another class extending from it? The book also says a class can be a superclass and a subclass at the same time. Does that mean I could have a superclass inside a superclass?

    And my final question is if my Superclass is BankAccount with deposit inside of it and I make a class named SavingsAccount extends BankAccount would I be able to do something like
    Java Code:
    mySavings.addAccount(new SavingsAccount(acctnum,inidepo);
    mySavings = mySavings.deposit(newDeposit);
    Without moving the deposit class into the SavingsAccount class?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    All classes are technically a subclass of some other class(Object), so all classes can be a super class and a sublass at the same time. That code snippet is confusing, it doesn't show enough to give you a solid answer. Like, what is newDeposit a variable for? what is mySavings?

    Yes another class is only a super class if it has a sub class. It wouldn't make sense to be a super class without any child classes. Think of a super class as a parent, and a sub class as a child. Is a person a parent if they don't have any children?

    Java Code:
    class X{} //the super class, subclass of Object
    class Y extends X{} //subclass of x. Super class of z
    class Z extends Y{} //subclass of y, and by extension x.
    When you call a constructor the first call is a call to the super classes constructor. Whether you type it or not. All class have a toString method, which is actually a method of the class Object which is inherited by all classes(children to Object).

    I hope I clarified it a bit, post back with anything I didn't do a good job explaining and I will try to clarify for you.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you could try to match a real world scenario it make sense to you. Consider three classes, Animal, Mammal and Cat. How about the Mammal category?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Oh, sunde887 give a technical explanation while typing mine. ;) Try to match them and let us know your thoughts.

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Sorry I omitted some details.

    newDeposit is an integer to be placed into the Savings Account balance.
    mySavings is a variable holding the Savings Account. I was looking at their example and it declares mySavings to a Bank dataclass. A simpler version would just be
    Java Code:
    mySavings = new SavingsAccount(acctnum,inidepo);
    Basically I was asking if I can reference the classes inside BankAccount such as deposit without copying them over to the SavingsAccount class.
    Last edited by Dark; 04-12-2011 at 10:21 AM.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Yeah I saw the animal, mammal, cat one on google and I undestand that much better now that it was explained a little to me. I'm just curious now as to whether or not I can access the classes inside the superclass from the subclass.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Dark View Post
    And my final question is if my Superclass is BankAccount with deposit inside of it and I make a class named SavingsAccount extends BankAccount would I be able to do something like
    Java Code:
    mySavings.addAccount(new SavingsAccount(acctnum,inidepo);
    mySavings = mySavings.deposit(newDeposit);
    Without moving the deposit class into the SavingsAccount class?

    when you make "a class named SavingsAccount extends BankAccount" then the class SavingsAccount inherits the fields and methods of BankAccount and if there is a deposit in BankAccount also the SavingAccount will inherit it without "moving" nothing.

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Alright thats what I was wondering because the book was showing me that I had to place the deposit segment into SavingsAccount but it offered no explanation. So if it just takes everything that was in BankAccount then I don't have to copy anything.

    Thanks guys, you saved me from a day long headache trying to comprehend what this book is trying to say.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What book are you reading?

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Java Concepts for Java 5 and 6. I can't stand it in the least but its the only one I have. I had it in PDF format before I deployed so I'm kindof stuck with it right now. If I try to order another one it could be a month before it gets to me.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    :( sorry to hear it's not that good. Don't forget the java tutorials, they may be more helpful than that book.

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Unfortunately, the only internet access I have is a Government line. Which has 99% of the internet blocked. I was lucky to even get to this site. The Oracle tutorials are blocked and categorized as "Education". Can you believe that?

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    lol, that's funny that they would block something classified as education. You need to find a way to download them onto your hard drive.

  14. #14
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well thats the even better thing. I'm on a government computer, which is the only type of computer that can access the internet. I program on my personal computer. I also can't move files to or from the government computer. Which pretty much screws me over.

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    lol. sadface

  16. #16
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Yeah, but its all good. I can access a few random sites including this one so I'll be able to continue programming.

Similar Threads

  1. Superclass question
    By keo in forum New To Java
    Replies: 6
    Last Post: 10-11-2010, 11:48 AM
  2. Class and SuperClass
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-05-2009, 10:55 AM
  3. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  4. Replies: 0
    Last Post: 11-12-2007, 11:53 AM

Posting Permissions

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