Page 1 of 1
Java Help
Posted: Thu Mar 28, 2013 2:18 pm
by Xaos
So i'm just making a thread, because I'm probably going to have some questions as I go on, as I am self-teaching AP Comp Science, which is basically java. Since I'm learning from a textbook, I have noone to ask questions. Hence, this thread. Now as for my questions....
Really, what is the point of abstract classes? A placeholder, but it just seems uneccessary esp. if you can call out of it anyway.
Re: Java Help
Posted: Thu Mar 28, 2013 7:59 pm
by Jackolantern
It is just a tool to build inheritance hierarchies. Abstract classes are simply a mix between POJOs (plain-old-Java-objects) and interfaces. It gives you the flexibility to implement methods that are generic enough inside the abstract class, while leaving some undefined that would be so specific that any implementation offered would be overridden in the subclass. They are essentially another tool, along with interfaces, that help get around Java's restriction to single inheritance.
That is why they exist. However, in practice few Java devs use them. Most applications just don't require the 20+ level deep hierarchies that would often contain abstract classes. But don't let their general lack of use stop you from using them if they are the right tool for the job.
Re: Java Help
Posted: Fri Mar 29, 2013 1:37 am
by Xaos
Jackolantern wrote:It is just a tool to build inheritance hierarchies. Abstract classes are simply a mix between POJOs (plain-old-Java-objects) and interfaces. It gives you the flexibility to implement methods that are generic enough inside the abstract class, while leaving some undefined that would be so specific that any implementation offered would be overridden in the subclass. They are essentially another tool, along with interfaces, that help get around Java's restriction to single inheritance.
That is why they exist. However, in practice few Java devs use them. Most applications just don't require the 20+ level deep hierarchies that would often contain abstract classes. But don't let their general lack of use stop you from using them if they are the right tool for the job.
THanks, that helps alot. Can you think of any examples where they would be useful? Seems easier to just declare and whatever before you use them and whatnot.
Re: Java Help
Posted: Fri Mar 29, 2013 2:46 pm
by Elvar
Xaos wrote:THanks, that helps alot. Can you think of any examples where they would be useful? Seems easier to just declare and whatever before you use them and whatnot.
At my study we built a roleplaying game, here we had a item class, which ex. Weapon, Key, Helm would inherit of. Basicly the item class had generic properties such as Title, Value, Rarity. But since there should never be a instance of just a item i.e a typeless item, we decided to make it abstract, therefore not to be instantiated.

Hope it cleared it up a bit.
Re: Java Help
Posted: Fri Mar 29, 2013 7:02 pm
by Jackolantern
Elvar wrote:Xaos wrote:THanks, that helps alot. Can you think of any examples where they would be useful? Seems easier to just declare and whatever before you use them and whatnot.
At my study we built a roleplaying game, here we had a item class, which ex. Weapon, Key, Helm would inherit of. Basicly the item class had generic properties such as Title, Value, Rarity. But since there should never be a instance of just a item i.e a typeless item, we decided to make it abstract, therefore not to be instantiated.

Hope it cleared it up a bit.
Ahh, yes that is another usage for abstract classes: classes too generic to be instantiated on their own. Java will not let you implement an abstract class.
As for other examples of their usage, say you were making a fighting game. You have a basic "Fighter" class and you tie the common special move control movement (like fireball motion with a punch, fireball motion with a kick, etc.) to methods on the Fighter class. However, anything you implement in those special move methods may be too generic to be of any use since those special moves will do something so different for each fighter, so anything you write in those methods will basically be a waste of time. And since you won't ever be instantiating a Fighter directly since it doesn't make sense, you could make the Fighter class abstract. You could then mark those special move methods as abstract in the Fighter class and implement them in each character class that inherits from Fighter.
Re: Java Help
Posted: Sat Mar 30, 2013 6:49 pm
by Xaos
Oh okay.....Thanks to both of you, I'm pretty sure I get it now. It's like a bowl where you can just pick up what you need in terms of methods and whatnot.
Re: Java Help
Posted: Sat Mar 30, 2013 8:04 pm
by Jackolantern
Xaos wrote:Oh okay.....Thanks to both of you, I'm pretty sure I get it now. It's like a bowl where you can just pick up what you need in terms of methods and whatnot.
Well, kind of. You either pick up
everything, and implement all abstract methods, or the class you just made is abstract as well. A class that can be instantiated cannot have
any abstract classes, and everything in the superclass is in the baseclass, so that means every abstract method gets carried over into your new class. If you don't implement every abstract method from the abstract class and make them concrete, you will get a compiler error unless you mark your new class as abstract as well.
Re: Java Help
Posted: Sun Mar 31, 2013 4:12 pm
by Xaos
Jackolantern wrote:Xaos wrote:Oh okay.....Thanks to both of you, I'm pretty sure I get it now. It's like a bowl where you can just pick up what you need in terms of methods and whatnot.
Well, kind of. You either pick up
everything, and implement all abstract methods, or the class you just made is abstract as well. A class that can be instantiated cannot have
any abstract classes, and everything in the superclass is in the baseclass, so that means every abstract method gets carried over into your new class. If you don't implement every abstract method from the abstract class and make them concrete, you will get a compiler error unless you mark your new class as abstract as well.
Oh okay. That's simple enough. Also, do you guys know any good (text) tuts that I can read along with my book?
Re: Java Help
Posted: Sun Mar 31, 2013 5:21 pm
by Jackolantern
There is always the
Really Big Java Tutorial. It pretty much covers everything. It is not really a "read end-to-end" tutorial, though. It really is really big lol.
Re: Java Help
Posted: Mon Apr 01, 2013 2:20 am
by Xaos
holy crap wow. I'll def look at that when I have questions, haha,