announcements‎ > ‎

2015-10-23 HW!

posted Oct 23, 2015, 8:03 AM by Samuel Konstantinovich   [ updated Oct 24, 2015, 4:15 AM ]
Abstract classes.

-A Java abstract class is a class which cannot be instantiated, meaning you cannot create new instances of an abstract class. The purpose of an abstract class is to function as a base for subclasses.

-An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes.

In Java you declare that a class is abstract by adding the abstract keyword to the class declaration. Here is a Java abstract class example:

public abstract class MyAbstractClass {

That is all there is to declaring an abstract class in Java. Now you cannot create instances of MyAbstractClass. Thus, the following Java code is no longer valid:

//not valid
MyAbstractClass myClassInstance =  new MyAbstractClass();  
//not valid

If you try to compile the code above the Java compiler will generate an error, saying that you cannot instantiate MyAbstractClass because it is an abstract class.

Abstract Methods

An abstract class can have abstract methods. You declare a method abstract by adding the abstract keyword in front of the method declaration. Here is a Java abstract method example:

public abstract class MyAbstractClass {
    public abstract void abstractMethod();

If a class has an abstract method, the whole class must be declared abstract. Not all methods in an abstract class have to be abstract methods. An abstract class can have a mixture of abstract and non-abstract methods.


Subclasses of an abstract class must implement (override) all abstract methods of its abstract superclass. The non-abstract methods of the superclass are just inherited as they are. They can also be overridden, if needed.

Here is an example subclass of the abstract class MyAbstractClass:

public class MySubClass extends MyAbstractClass {

    public void abstractMethod() {
        System.out.println("My method implementation");
Notice how MySubClass has to implement the abstract method abstractMethod() from its abstract superclass MyAbstractClass.

The only time a subclass of an abstract class is not forced to implement all abstract methods of its superclass, is if the subclass is also an abstract class.


We should make Actor abstract. 

Then it will start to make sense...

get methods will be concrete methods of the abstract class
attack/specialAttack will be abstract methods.

... lets try doing that now and see what else we can do.

HW Use this abstract Actor class to update your sub-classes: (sorry for delayed posting! If there is a legitimate problem due to the late post let me know right away)
public abstract class Actor{
    private String name;
    private int strength,agility,intelligence;
    private int health;

    //abstract methods
    public abstract String attack(Actor other);
    public abstract String specialAttack(Actor other);
    public abstract String getResourceName();
    public abstract int getResource();

    public Actor(String name, int str, int agi, int intel, int health){ = name;
        agility = agi;
        strength = str;
        intelligence = intel; = health;

    public Actor(String name){

    public Actor(){

    //concrete methods
    public String toString(){
        return name + " hp: "+health+ "\n STR:"+strength+" AGI:"+agility+" INT:"+intelligence+"\n "+getResourceName()+ ": "+getResource();

    public String getName(){return name;}
    public int getInt(){return intelligence;}
    public int getStr(){return strength;}
    public int getAgi(){return agility;}
    public int getHealth(){return health;}

    public int takeDamage(int damage){
        health -= damage;
        return damage;

    //placeholder for more features
    public String defend(){
        return name+" defends against incoming attacks";

    //placeholder for more features
    public void nextTurn(){
        //reset any temporary things like defend