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

2017-10-19

posted Oct 18, 2017, 6:24 PM by Konstantinovich Samuel   [ updated Oct 19, 2017, 4:28 PM ]
Class Discussion Board:
https://groups.google.com/a/stuy.edu/forum/#!forum/apcs2017-konstantinovich

You need to be logged in with your stuy.edu account.

You should also 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 that11111 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!*


abstract class Player{
  private int health;
  private String name;

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

  public int getHealth(){
   return health;
  }

  public String getName(){
   return title + name;
  }

  public String title(){
    return "";
  }

  
public abstract void attack(Player other);

  public String info(){
    return "Error - lazy programmer detected (write a proper description)";
  }
}

class Barbarian extends Player{
    //You automatically inherit the accessors yay free methods.

    //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 "Destroyer ";
    }

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

class Mage extends Player{
    //You automatically inherit the accessors yay free methods.

    //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 warrior that fights with brute strength and pure rage.";
    }
}


WHY?!?!?! WHY WOULD WE DO THIS?!?!?!
-Discuss with your neighbors in groups of 4, and list the benefits of being able to design with an abstract class.
Come up with reasons and/or examples.





Comments