Results 1 to 3 of 3
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By DarrylBurke

Thread: Abstract Classes... Do I understand this?

  1. #1
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default 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.

    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:
    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();
    	
    }
    This is one of my sub-classes that extend charGen:

    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);
    	}
    
    }
    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.

    Just need someone to say "Nope... you got it all wrong", or "Yup... pretty much sums it up".

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    Jingoism likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default 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

    db
    Jingoism likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help with Abstract Classes please.
    By fatabass in forum New To Java
    Replies: 1
    Last Post: 03-23-2012, 03:26 AM
  2. Abstract classes
    By FadedAura in forum New To Java
    Replies: 4
    Last Post: 11-21-2011, 10:16 PM
  3. Abstract Classes.
    By maknib in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 02:30 PM
  4. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 08:31 AM
  5. Inverfaces vs Abstract Classes
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 09: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
  •