Home Segments Top Top Previous Next

251: Mainline

To specify a class's superclass, you insert the keyword extends, and the name of the superclass, just after the name of the class in the class's definition.

For example, as you define the Movie class and the Symphony class, you specify that Attraction is the superclass by inserting the keyword extends, followed by Attraction, just after the class name:

public class Movie extends Attraction { 
 // Define instance variables: 
 public int script, acting, direction; 
 // Define zero-parameter constructor: 
 public Movie () { 
  System.out.println("Calling zero-parameter Movie constructor"); 
  script = 5; acting = 5; direction = 5; 
 } 
 // Define three-parameter constructor: 
 public Movie (int s, int a, int d) {  
  script = s; acting = a; direction = d;  
 }  
 // Define rating: 
 public int rating () { 
  return script + acting + direction; 
 } 
} 

public class Symphony extends Attraction { 
 // Define instance variables: 
 public int music, playing, conducting; 
 // Define zero-parameter constructor: 
 public Symphony () { 
  System.out.println("Calling zero-parameter Symphony constructor"); 
  music = 5; playing = 5; conducting = 5; 
 } 
 // Define three-parameter constructor: 
 public Symphony (int m, int p, int c) {  
  music = m; playing = p; conducting = c;  
 }  
 // Define rating: 
 public int rating () { 
  return music + playing + conducting; 
 } 
}