a-kind-of Person

The Prof class is a subclass of the Person class
a subclass will inherit all the attributes and methods of its superclass(Inheritance); in this case, Prof will inherit name, age, height, sex, and drawPerson from Person; and we only need to declare salary, drawBBoard and drawProf for the Prof class

//filename: Prof.java
import java.awt.*;
public class Prof extends Person
{
  // attributes of Prof as shown below
  public double salary;
  // methods of Prof
  public void drawBBoard(Graphics g, int x, int y) {
    // Draw the Black Board
    // Draw a brown colored rectangle
    Color c = new Color(200, 165, 135);
    g.setColor(c);
    g.fillRect(x, y, 80, 65);
    // Draw a black colored inner rectangle
    g.setColor(Color.black);
    g.fillRect(x + 4, y + 4, 72, 57);
  }
  public void drawProf(Graphics g, int x, int y) {
    drawPerson(g, x, y);
    drawBBoard(g, x + 25, x + 2);
  }
}

//ProfApplet.java
import java.awt.*;
import java.applet.Applet;
public class ProfApplet extends Applet {
  public Prof joe; // Joe is an instance variable.
  public void init() {
    /* init() is called only once.
    This is where the applet is initialized.*/
    joe = new Prof(); // Create a new object instance for Joe Hamilton.
  }
  public void paint ( Graphics g ) {
    joe.drawProf(g, 20, 15); // Draw Joe on the applet.
  }
}

Previou page Next page