Click on inherit.java to get source.
/**
@path JavaExample/Inherit/
demo of constructors, destructors, inheritance, etc.
this is the Java version of the C++ example inherit.cpp
@author Erich Kaltofen
@version Oct 11, 1998
*/
import java.io.*;
public class inherit {
public static void main(String [] args)
{ // Java 1.1 character I/O (PrintStream is deprecated)
PrintWriter out = new PrintWriter(System.out, true); // auto-flush
out.println("0. base b = new base(5);");
base b = new base(5);
out.println(b.toString()); // tests toString in base class
out.println();
// 1. create a derive object
out.println("1. d1 = new derive(5,20);");
derive d1 = new derive(5,20);
d1.print(out); out.println(); out.println();
// 2. create a derive object with default constructor
out.println("2. derive d2 = new derive();");
derive d2 = new derive();
d2.print(out); out.println(); out.println();
// 3. assign a derive object to another
out.println("3. d2 = d1;");
d2 = d1; // old d2 will be garbage collected
d2.print(out); out.println(); out.println();
// 4. create a derive object by copy
out.println("4. derive d3 = d2.clone();");
derive d3 = (derive)d2.clone();
d3.print(out); out.println(); out.println();
out.println("The end");
}
}