Can't Instantiate an Abstract Class

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

The parent class Card is an abstract class and therefore cannot be instantiated.

Can't Instantiate an Abstract Class

You can't do the following:

   . . . .

  public static void main ( String[] args )
  {
     . . . . 

    Card card = new Card() ;        // can't instantiate abstract class
    card.greeting() ;

     . . . .
  }

Because Card is an abstract class, the compiler flags this as a syntax error. Card does have a constructor that is (implicitly) invoked by its children, but it cannot be invoked directly. However, the following is OK:

   . . . .

  public static void main ( String[] args ) 
  {
     . . . . 

    Card card = new Valentine( "Joe", 14 ) ;      // a Valentine is-a Card
    card.greeting() ;

     . . . .
  }

It is OK to save a reference to a Valentine object in a reference variable of type Card because Valentine is-a Card. You can think of the reference variable Card as being a card rack designed to hold any type of a Card.

QUESTION 11:

Do you think it would be OK to do the following?

Card card2 = new Holiday( "Bob" ) ; 
Card card3 = new Birthday( "Emily" ) ;