Results 1 to 3 of 3
- 11-16-2012, 07:16 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
Abstract Classes... Do I understand this?
Hello all!
I watched multiple tutorials on abstract classes, and felt that I understood... just not quite sure. I made a small text based game to test out this understanding, and it works quite well... just wondering if I am grasping the idea or just shoehorning my own idea of what it should be into it. Alright starting to confuse myself already..gif)
Right now the classes that extend my abstract class just have useless methods, but those are really just a test to see if it would work ( except resetHP which is actually useful ).
This is my abstract class:
This is one of my sub-classes that extend charGen:Java Code:public abstract class charGen { private String namez; private int HP; private int STR; private float AGI; public charGen(String name, int hp, int str, float agi){ this.namez = name; this.HP = hp; this.STR = str; this.AGI = agi; } public String getName(){ return this.namez; } public int getHP(){ return this.HP; } public int getSTR(){ return this.STR; } public float getAGI(){ return this.AGI; } public void setName(String n){ this.namez = n; } public void setHP(int h){ this.HP = h; } public void setSTR(int s){ this.STR = s; } public void setAGI(float a){ this.AGI = a; } public abstract String specialSTR(); public abstract String getRace(); public abstract void resetHP(); }
I find the abstract class is useful for me since it handles all the simple methods that are common to all of my sub-classes, but can defer to the sub-classes when the method is not common to all of them.Java Code:public class charOrc extends charGen{ public charOrc(String n, int h, int s, float a) { super(n,h,s,a); } public String specialSTR(){ return super.getName() + " erupts into a vicious Orc frenzy!"; } public String getRace(){ return "Black Orc"; } public void resetHP(){ super.setHP(150); } }
Just need someone to say "Nope... you got it all wrong", or "Yup... pretty much sums it up".
- 11-16-2012, 07:32 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Abstract Classes... Do I understand this?
Yup... pretty much sums it up; a class is abstract if you declare it as such; note that it doesn't need to have abstract methods to be an abstract class.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2012, 09:35 PM #3
Re: Abstract Classes... Do I understand this?
You need to conform to the Java coding conventions: class names start with an uppercase letter.
Code Conventions for the Java Programming Language: Contents
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Help with Abstract Classes please.
By fatabass in forum New To JavaReplies: 1Last Post: 03-23-2012, 03:26 AM -
Abstract classes
By FadedAura in forum New To JavaReplies: 4Last Post: 11-21-2011, 10:16 PM -
Abstract Classes.
By maknib in forum New To JavaReplies: 3Last Post: 05-12-2011, 02:30 PM -
abstract classes
By renju krishnan in forum New To JavaReplies: 1Last Post: 09-29-2010, 08:31 AM -
Inverfaces vs Abstract Classes
By ravian in forum New To JavaReplies: 1Last Post: 11-28-2007, 09:53 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks