====== The Object Class, Abstract Classes, Interfaces, Polymorphism ====== ---- * The topics covered here are related to inheritance. ---- ===== The Object Class ===== * Every class in Java is descended from the //java.lang.Object// class. * If no inheritance is specified when a class is defined, the superclass of the class is //Object// by default. * Every class, whether superclass or subclass, inherits from the //Object// class in Java. * Two notable methods that every class inherits from //Object// are //toString// and //equals//. * A third method that every class inherits from //Object// is //clone// ([[http://en.wikipedia.org/wiki/Clone_%28Java_method%29|Link]]) ---- ===== Abstract Classes ===== * An abstract class is used solely for creating subclasses. * You can't instantiate an object of an abstract class using //new//. * So, an abstract class is usually a highly general-sounding class, like //GeometricObject// or //Student//. * See the Student <- CompSciStudent abstract class inheritance example. * If a class contains an //abstract// method (like the //Student// class does), __the class itself has to be declared abstract__. * //Abstract// methods are declared, __but not defined__, e.g. the //Student// class' //getRemainingHours()// method. * An abstract class' subclasses __MUST__ include and override the abstract methods in the abstract superclass. * So, the //CompSciStudent// subclass MUST contain a definition of the //getRemainingHours()// method or else an error will occur. * In this way, an abstract superclass can impose certain restrictions (or //requirements//) on what its subclasses can do. ---- ===== Interfaces ===== * An interface is similar to an abstract class in that a class that implements an interface is __REQUIRED__ to define certain methods. * An interface must only contain constants and abstract methods, no variables. * The intent of interfaces is to ensure the __behavior__ of objects. * See the GradedActivity <- FinalExam3 Relatable interface example * GUI Example: DogYearsListen.java // The ActionListener interface contains only one abstract // method called actionPerformed(). // Because the class ConvertBtnListener implements the // ActionListener interface, it is required to define the // actionPerformed() method. class ConvertBtnListener implements ActionListener { public void actionPerformed(ActionEvent e) { ---- ===== Polymorphism ===== * One of the foundations of object-oriented programming (along with encapsulation and inheritance). * Related to inheritance * A subclass object __is an__ object of its superclass, but not vise versa. * In the [[cs176/501b/triangle_inheritance_example|GeometricObject1 <- Triangle]] example, when you construct the [[http://rockhopper.monmouth.edu/~jchung/cs176/su14/src/Triangle/testTriangle.java|Triangle object tri]], //tri// __is a__ Triangle. At the same time, //tri// __is a__ GeometricObject1. * If you construct a GeometricObject1 object //geo//, //geo// __is a__ GeometricObject1, but //geo// is not a Triangle. * Polymorphism means that an object of a subclass can be used wherever its superclass object is used. * Example: (main class only) // Assume that the GeometricObject1, Circle4 and // Rectangle1 classes are defined already: public class PolymorphismDemo { public static void main(String[] args) { // Display Circle and rectangle properties displayObject(new Circle4(1, "red", false)); displayObject(new Rectangle1(1, 1, "black", true)); } /** Method to display geometric object properties */ // (POLYMORPHISM BEING EMPLOYED HERE:) public static void displayObject(GeometricObject1 object) { System.out.println("Created on " + object.getDateCreated() + ". Color is " + object.getColor()); } } * The above example demonstrates that you can always pass an instance of a subclass (Circle4 or Rectangle1) to a parameter of its superclass (GeometricObject1). * Example: public class DynamicBindingDemo { public static void main(String[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } // (POLYMORPHISM BEING EMPLOYED HERE:) public static void m(Object x) { System.out.println(x.toString()); } } class GraduateStudent extends Student { } class Student extends Person { public String toString() { return "Student"; } } class Person extends Object { public String toString() { return "Person"; } } * See also the methods in the //Relatable// interface above. * The //equals, isGreater// and //isLess// methods take one //GradedActivity// object as an input parameter, but any subclass of //GradedActivity// such as //FinalExam3// can use the three methods because of the property of polymorphism. public interface Relatable { public abstract boolean equals(GradedActivity g); public abstract boolean isGreater(GradedActivity g); public abstract boolean isLess(GradedActivity g); } * Polymorphism can help to reduce the number of listeners that are required for GUI components. * Example: FractionGUI ----