Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Understanding Abstract Classes... am I getting this?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Understanding Abstract Classes... am I getting this?

    New JAVA programmer here, and I have looked online at multiple tutorials about abstract classes and managed to get them to work successfully... just not sure I have this quite right.

    I will use my current implementation as an example.

    This is my abstract class:

    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 classes that extends charGen:

    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 curious if this is the proper way to use an abstract class? I find it handy since it handles all the simple methods that my different sub-classes have in common, but will relegate the particulars back to those classes to handle.

    Did that make sense?


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Understanding Abstract Classes... am I getting this?

    Yes, the usage is correct. One side note, you do not need to prefix the calls with "super" when you call getName() and setHP() as there is only one instance of these methods. It would make more sense to prefix a call with super if you had overridden the method and you want to call the parent method.

    As an example. Say the parent class defines a toString() method and you implement a subclass. The subclass wants to implement the toString() method and add any new fields it may have defined to it so you may do something like the following:

    parents toString
    public String toString() { return "Parent"; }
     
    child toString
    public String toString() {
    return super.toString() + " Child";
    }

Similar Threads

  1. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 11:35 AM
  2. Difficulty with understanding interaction between Classes
    By JBRPG in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2012, 07:16 AM
  3. NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!
    By BlackShadow in forum Java Theory & Questions
    Replies: 1
    Last Post: April 6th, 2012, 10:11 PM
  4. NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!
    By BlackShadow in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2012, 09:29 AM
  5. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM