Title: Music.java
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Normal classes, Abstract Classes, and Interfaces

Music3.java - Normal Class : Instrument3


//: Music3.java
// Deriving from a base class (Extend)
// Note which play method gets called.  Especially note the last instrument.

import java.util.*;

class Instrument3 {
  public void play() {
    System.out.println("Instrument3.play()");
  }
  public String what() {
    return "Instrument3";
  }
  public void adjust() {}
}

class Wind3 extends Instrument3 {
  public void play() {
    System.out.println("Wind3.play()");
  }
  public String what() { return "Wind3"; }
  public void adjust() {}
}

class Percussion3 extends Instrument3 {
  public void play() {
    System.out.println("Percussion3.play()");
  }
  public String what() { return "Percussion3"; }
  public void adjust() {}
}

class Stringed3 extends Instrument3 {
  public void play() {
    System.out.println("Stringed3.play()");
  }
  public String what() { return "Stringed3"; }
  public void adjust() {}
}

class Brass3 extends Wind3 {
  public void play() {
    System.out.println("Brass3.play()");
  }
  public void adjust() {
    System.out.println("Brass3.adjust()");
  }
}

class Woodwind3 extends Wind3 {
  public void play() {
    System.out.println("Woodwind3.play()");
  }
  public String what() { return "Woodwind3"; }
}

public class Music3 {
  // Doesn't care about type, so new types
  // added to the system still work right:

  static void playAll(Instrument3[] e) {
    Instrument3 instrument;
    for(int i = 0; i < e.length; i++)
       {
           instrument = e[i];
           instrument.play();
       }
  }
  public static void main(String[] args) {
    Instrument3[] orchestra = new Instrument3[6];
    int i = 0;
    // Upcasting during addition to the array:
    orchestra[i++] = new Wind3();
    orchestra[i++] = new Percussion3();
    orchestra[i++] = new Stringed3();
    orchestra[i++] = new Brass3();
    orchestra[i++] = new Woodwind3();
    orchestra[i++] = new Instrument3();

    playAll(orchestra);
  }
}




Music4.java - abstract class: Intstrument4


//: Music4.java
// Abstract classes and methods
// Note that Instrument4 cannot be instantiated anymore
// because it's an Abstract class. All of the abstract methods
// must be overriden in any derived class (i.e play() in this case).
//  On the otherhand, the non-Abstract classes can optionally be overridden.
//  Look at tha adjust method to see this in action.

import java.util.*;

abstract class Instrument4 {
  int i; // storage allocated for each
  public abstract void play();
  public String what() {
    return "Instrument4";
  }
  public  void adjust()  {}
}

class Wind4 extends Instrument4 {
  public void play() {
    System.out.println("Wind4.play()");
  }
  public String what() { return "Wind4"; }
}

class Percussion4 extends Instrument4 {
  public void play() {
    System.out.println("Percussion4.play()");
  }
  public String what() { return "Percussion4"; }
  public  void adjust()  { System.out.println("Percussion4 adjust");}

}

class Stringed4 extends Instrument4 {
  public void play() {
    System.out.println("Stringed4.play()");
  }
  public String what() { return "Stringed4"; }
}

class Brass4 extends Wind4 {
  public void play() {
    System.out.println("Brass4.play()");
  }
  public void adjust() {
    System.out.println("Brass4.adjust()");
  }
}

class Woodwind4 extends Wind4 {
  public void play() {
    System.out.println("Woodwind4.play()");
  }
  public String what() { return "Woodwind4"; }
}

public class Music4 {

  static void playAll(Instrument4[] e) {
   Instrument4 instrument;
    for(int i = 0; i < e.length; i++)
       {
       instrument = e[i];
       instrument.play();
       }
  }
  public static void main(String[] args) {
    Instrument4[] orchestra = new Instrument4[5];
    int i = 0;
    // Upcasting during addition to the array:
    orchestra[i++] = new Wind4();
    orchestra[i++] = new Percussion4();
    orchestra[i++] = new Stringed4();
    orchestra[i++] = new Brass4();
    orchestra[i++] = new Woodwind4();
    playAll(orchestra);
       // Can't do the following because Instrument4 is
       // an abstract class:
       //!   test = new Instrument4();

  }
} ///:~



Music5.java - interface: Instrument5


//: Music5.java
// Interfaces
// Instrument5 acts like a class in some ways, but it can't be
// instantiated.  Also all of its member functions act like they
// are abstract and must be overridden (unlike the abstract case).
// One big advantage of Interfaces over abstract classes is that
// you can get the equivalence of multiple inheritances.  Note that
// have added a Singer class that implements Instrument5 and is
// derived from a Person class.

import java.util.*;

interface Instrument5 {
  // Compile-time constant:
  int i = 5; // static & final
  // Cannot have method definitions:
  void play(); // Automatically public
  String what();
  void adjust();
}
// *******************************
 class Person{
    int age;
    int height;
    void talk(String words)
        {
            // Whatever we want to do here
        }
  }
// *******************************
class Singer extends Person implements Instrument5{

    public void play(){
        System.out.println("Singer.play()");
    }
  public String what() { return "Singer"; }
  public void adjust() {}
}

// What is Wind5 derived from now???
class Wind5 implements Instrument5 {
  public void play() {
    System.out.println("Wind5.play()");
  }
  public String what() { return "Wind5"; }
  public void adjust() {}
}

class Percussion5 implements Instrument5 {
  public void play() {
    System.out.println("Percussion5.play()");
  }
  public String what() { return "Percussion5"; }
  public void adjust() {}
}

class Stringed5 implements Instrument5 {
  public void play() {
    System.out.println("Stringed5.play()");
  }
  public String what() { return "Stringed5"; }
  public void adjust() {}
}

class Brass5 extends Wind5 {
  public void play() {
    System.out.println("Brass5.play()");
  }
  public void adjust() {
    System.out.println("Brass5.adjust()");
  }
}

class Woodwind5 extends Wind5 {
  public void play() {
    System.out.println("Woodwind5.play()");
  }
  public String what() { return "Woodwind5"; }
}

public class Music5 {
  // Doesn't care about type, so new types
  // added to the system still work right:

  static void playAll(Instrument5[] e) {
   Instrument5 instrument;
    for(int i = 0; i < e.length; i++)
       {
           instrument = e[i];
           instrument.play();
       }
  }
  public static void main(String[] args) {
    Instrument5[] orchestra = new Instrument5[6];
    int i = 0;
    // Upcasting during addition to the array:
    orchestra[i++] = new Wind5();
    orchestra[i++] = new Percussion5();
    orchestra[i++] = new Stringed5();
    orchestra[i++] = new Brass5();
    orchestra[i++] = new Woodwind5();
    orchestra[i++] = new Singer();  // Singer implements Instrument5
    playAll(orchestra);
  }
} ///:~



Last Updated: September 2, 2002 4:24 PM