Shape s1 = new Circle(); Shape s2 = new Rectangle(); Shape s3 = new Triangle();
s1.findArea(); // runs the findArea() method from the Circle class s2.findArea(); // runs the findArea() method from the Rectangle class s3.findArea(); // runs the findArea() method from the Triangle class // if any of these subclasses did not override the finArea() method, // then the Shape class' findArea() method will run
// Sample method public int draw(Shape s) { // definition code } // sample calls Shape s1 = new Shape(); Shape s2 = new Circle(); Shape s3 = new Rectangle(); draw(s1); // normal usage draw(s2); // passing in a Circle object draw(s3); // passing in a Rectangle object
Example: Assume the setup in the previous example, base class Shape and derived classes Rectangle, Circle, Triangle. Suppose the base class has a findArea() method (probably abstract, since we don't know how to compute the area for a generic shape), and each derived class has its own findArea() method.
Shape[] list = new Shape[size]; // create an array of Shape reference variables list[0] = new Circle(); // attach a Circle to first array slot list[1] = new Rectangle(); // attach a Rectangle to second slot list[2] = new Triangle(); // attach a Triangle to third slot // .... continue in a like manner, attaching a variety of different // shapes to the array. Note that there could be MANY subcategories // of shapes and MANY array elements. Notice that we are using // the polymorphism feature for (int i = 0; i < list.length; i++) System.out.println("The area of shape # " + i + " = " + list[i].findArea())Note that in this for-loop, the appropriate area methods are called for each shape attached to the array, without the need for separate storage for different shape types (i.e. no need for an array of circles, and a separate array of rectangles, etc).
Shape s1, s2; // Shape is the base class Circle c; // Circle is a derived class s1 = new Circle(); // automatically legal (as seen above) s2 = c; // automatically legal s1 = (Shape)c; // explicit cast used, but equivalent to aboveTo convert an instance of a superclass (base) to an instance of a subclass (derived), the explicit cast operation must be used:
c = s1; // would be illegal -- cast needed c = (Circle)s1; // legal (though not always so useful)
Shape s1; Circle c1; // other code..... if (s1 instanceof Circle) c1 = (Circle)s1; // cast to a Circle variable
modifier interface Name { // constant declarations // abstract method signatures - keyword "abstract" not needed // for these. ALL methods in an interface are abstract }Use the keyword implements to state that a class will use a certain interface. In this example, Comparable is the name of an interface. The class ComparableCircle inherits the data from the Comparable interface, and would then need to implement the methods (to be able to use them).
class ComparableCircle extends Circle implements Comparable { // .... }Other rules:
public interface NewInterface extends interface1, ..., interfaceN
public class NewClass extends BaseClass implements interface1, ..., interfaceN
public interface Cloneable { }This is a marker interface -- no data or methods, but special meaning in Java. A class can use the clone() method (inherited from class Object) only if it implements Cloneable.