The abstract Keyword


An abstract method belongs to an abstract class, and it does not have a body. The body is provided by the subclass:

MyAbstract.java (the abstract keyword)
class MyAbstract {
  public static void main( String[ ] args ) {
    // Create an object of the Student class, 
    // which inherits attributes and methods from Person.
    Student myStudent = new Student( args[0] );
    myStudent.graduationYear = Integer.parseInt( args[1] );
    System.out.print( myStudent.fname + " " );
    System.out.print( myStudent.graduationYear + " " );
    myStudent.study( );     // call abstract method
  }
}
Person.java (the abstract keyword)
// Abstract class
abstract class Person {
  public String fname;
  Person( String name ) { fname = name; }     // Constructor
  public abstract void study( );              // Abstract method
}
Student.java (the abstract keyword)
// Subclass (inheriting from Person)
class Student extends Person {
  public int graduationYear;
  // Constructor
  Student( String name ) { super( name ); }
  // The body of the abstract method is provided here.
  public void study( ) { System.out.print( "24/7" ); }
}
shell> java MyAbstract    

Output:           Result:




      Asking my husband to help out around the house is like    
      banging my head against the wall (annoying).