Java Help

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Java Help

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Java Help

Post 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.
The indelible lord of tl;dr
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Java Help

Post 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.
User avatar
Elvar
Posts: 86
Joined: Sun Oct 07, 2012 7:04 pm

Re: Java Help

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Java Help

Post 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.
The indelible lord of tl;dr
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Java Help

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Java Help

Post 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.
The indelible lord of tl;dr
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Java Help

Post 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?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Java Help

Post 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.
The indelible lord of tl;dr
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Java Help

Post by Xaos »

holy crap wow. I'll def look at that when I have questions, haha,
Post Reply

Return to “Beginner Help and Support”