Results 1 to 16 of 16
Thread: Superclass Question
- 04-12-2011, 10:02 AM #1
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
Without moving the deposit class into the SavingsAccount class?Java Code:mySavings.addAccount(new SavingsAccount(acctnum,inidepo); mySavings = mySavings.deposit(newDeposit);
- 04-12-2011, 10:11 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
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).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.
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.
- 04-12-2011, 10:13 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 04-12-2011, 10:14 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Oh, sunde887 give a technical explanation while typing mine. ;) Try to match them and let us know your thoughts.
- 04-12-2011, 10:17 AM #5
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
Basically I was asking if I can reference the classes inside BankAccount such as deposit without copying them over to the SavingsAccount class.Java Code:mySavings = new SavingsAccount(acctnum,inidepo);
Last edited by Dark; 04-12-2011 at 10:21 AM.
- 04-12-2011, 10:19 AM #6
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.
- 04-12-2011, 10:22 AM #7
- 04-12-2011, 10:28 AM #8
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.
- 04-12-2011, 10:31 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What book are you reading?
- 04-12-2011, 10:33 AM #10
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.
- 04-12-2011, 10:34 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
:( sorry to hear it's not that good. Don't forget the java tutorials, they may be more helpful than that book.
- 04-12-2011, 10:36 AM #12
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?
- 04-12-2011, 10:42 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-12-2011, 10:44 AM #14
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.
- 04-12-2011, 10:45 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
lol. sadface
- 04-12-2011, 11:40 AM #16
Similar Threads
-
Superclass question
By keo in forum New To JavaReplies: 6Last Post: 10-11-2010, 11:48 AM -
Class and SuperClass
By pompeez in forum New To JavaReplies: 2Last Post: 08-05-2009, 10:55 AM -
superclass and subclass
By mr idiot in forum New To JavaReplies: 19Last Post: 01-03-2009, 07:29 AM -
Getting name of superclass (Example: ArithmeticException)
By Java Tip in forum Java TipReplies: 0Last Post: 11-12-2007, 11:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks