CS110 Notes Prepared by – Namita Singla 5/13/2005 Interfaces "An Interface is a specialized class declaration that can declare constants and method declarations, but not method implementations". Interfaces cannot be instantiated. Classes that implement the interface must provide the methods implementation described by the interface. Method implementations cannot be provided (i.e. only method definitions, no method body) in an interface. All methods declared in an interface are public (even if public is not specified). They may not be protected or private. The implements keyword is used to indicate that a class implements an interface e.g. public class X implements Y (Y is an interface) A class may implement multiple interfaces. e.g. public class X implements Y, Z (Y and Z are interfaces) Interfaces may have sub-interfaces. It is possible to have an interface reference. This can reference objects that implement the interface. Example 1 // NameComparator implements Comparator interface Comparator c = new NameComparator(); Example 2 // Pixel implements Comparable interface Comparable comp = new Pixel(); Inheritance In the real world we use inheritance all of the time, we reuse and enhance existing ideas or objects to make new ones. Cars, houses and computers are not created from scratch, they are built from known components or ideas. We make use of existing objects and enhance them because it is usually more efficient to do so, resulting in greater reliability and decreased cost. Inheritance is a form of reuse leading to increased productivity, integrity and consistency. Inheritance provides the capability to create new objects that maintain the attributes and operations of ancestor objects The class from which a class inherits is known as the base class (or superclass) The class which inherits is known as a derived class (or subclass) Inheritance works exactly like a hierarchy with one class from which all others are derived (Object) Normally superclasses provide methods and data that are common to all derived classes - they are generalized Subclasses provide the methods and data which differentiate them - they are specialized. Subclasses may: o add attributes (fields) o add methods o override methods from super classes extends is used to specify the base class (superclass) a new derived class (subclass) is derived from. If nothing is specified Object is used as the base class. The ultimate ancestor (superclass) of all classes is Object. In Java this and super are mainly used for resolving name conflicts or clarification. Abstract Classes Abstract classes have one or more abstract methods and can not be instantiated. The class may also have attributes and non-abstract methods. Abstract methods are not implemented; all that is supplied is the method header. To use an abstract method, it must be subclassed and the all of the abstract methods implemented. An abstract class cannot be instantiated. A class with one or more abstract method is abstract and must be declared as abstract. Polymorphism Polymorphism means: many shapes. Ability of superclass reference to denote objects of its own class and sub classes at runtime is called polymorphism. In other words polymorphism is the facility for the same method name to be used in conjunction with compatible objects. Put another way, a method can have multiple implementations (method overriding), one of which is selected when the program is running depending on the type of the object involved. Polymorphism is also called dynamic binding, late binding or run-time binding. Polymorphism is not possible without inheritance. Overloading is NOT polymorphism. Overriding Methods A method in base class is said to be overridden if a method in subclass has an identical signature (same name, parameter types and order) and return type as that of a method in a base class. Overloading Methods A method in a class is said to be overloaded if another method in the same class has the same name but different number, type or order of parameters. The overloaded methods must have a different signature - usually this means they must have different number, type or order of parameters. The return type does not count as a difference.