Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎

2018-10-18

posted Oct 18, 2018, 5:39 AM by Konstantinovich Samuel   [ updated Oct 18, 2018, 6:22 AM ]
You should be reading all of the google group posts. 
-If you understand : you can answer a question.
-If you don't understand: you can learn from an answer. 
-If there is no answer, say that you are having a similar problem!

AT HOME:
You should familiarize yourself with gmail filters, to put things into "labels" automatically. Labels are like folders, but you can have an email in multiple labels. 
I suggest a COMPSCI label of something of that nature so you can see CompSci (32)  which means you have 32 unread messages regarding your CS class!

Goal: inheritance part III: what if there is no concrete superclass?!?!?!


Concrete:  Concrete classes CAN be instantiated, but we do not need a keyword for it. This is all the classes you have seen so far! This is the opposite of abstract. 

New Keyword:  abstract

Abstract class - A class that cannot be instantiated. This is intended to be used as a base class for other classes!

Abstract method - A method that can ONLY be declared inside an abstract class. It has no body and ends in a semicolon. It must be completed in any class that extends the Abstract class.

*There are more rules about abstract classes but we will not worry about those for now!*


public class Driver{

  public static void main(String[]args){

    Barbarian b = new Barbarian();

    Wizard w = new Wizard();

    //NOT VALID: Player p = new Player();

  }

}

/*********************************************************************/
/*********************************************************************/
/*********************************************************************/

public abstract class Player{

  private int health;
  private String name;

  //DOES NOT EXIST:
  //public Player()

  public Player(int hp, String n){
   health = hp;
   name = n;
  }

  public int getHealth(){
   return health;
  }
  /*
  Abstract methods! They must be written in any concrete subclass.
  */

  
  //reduce the HP of the other player and print
  //flavor text to describe the behavior.
  public abstract void attack(Player other);

  //return a themed title of the player
public abstract String title();
  
  //return a description of the class
  public abstract String info();

  //you can call abstract methods in concrete methods because 
  //you know they will be implemented in subclasses!
  public String getName(){
   return title() + name;
  }

}

/*********************************************************************/
/*********************************************************************/
/*********************************************************************/

public class Barbarian extends Player{
  //You should choose the proper super constructor!
  public Barbarian(){
    super(30,"Conan");
  }

    //You MUST implement attack in order for this class to compile.
    public void attack(Player other){
      //Do something
    }
    //You MUST implement title
    public String title(){
      return "Destroyer ";
    }

    public String info(){
     return "A warrior that fights with brute strength and pure rage.";
    }
}

/*********************************************************************/
/*********************************************************************/
/*********************************************************************/

public class Wizard extends Player{
    //You should choose the proper super constructor!
    //comment both constructors out and see what happens. Why?
    public Wizard(){
      super(17,"Mervin");
    }
    //You MUST implement attack in order for this class to compile.
    public void attack(Player other){
      //Do something
    }

    //You MAY choose to override title + info
    public String title(){
      return "Grand Sage ";
    }

    public String info(){
     return "A super calous fragile mystic vexed by halitosis.";
    }
}


Comments