go to previous page   go to home page   go to next page

Answer:

Yes — by using an abstract class a programmer can make all children of that class look alike in important ways.


Not Everything in an abstract Class is abstract

Not everything defined in an abstract classes must be abstract. The variable recipient is defined in Card and inherited in the usual way. However, if a class contains even one abstract method, then the class itself has to be declared to be abstract. Here is a program to test the two classes.

abstract class Card
{
  protected String recipient;
  public abstract void greeting();
}

class Holiday extends Card
{
  public Holiday( String r )
  {
    recipient = r;
  }

  public void greeting()
  {
    System.out.println("Dear " + recipient + ",");
    System.out.println("Season's Greetings!\n");
  }
}

public class CardTester
{
  public static void main ( String[] args )
  {
    Holiday hol = new Holiday("Santa");
    hol.greeting();
  }
}

This is a complete, runnable program. The urge to copy it to a file and run it must be irresistible. Call the file CardTester.java .


QUESTION 6:

Could you write this program without using an abstract class?


go to previous page   go to home page   go to next page