Академический Документы
Профессиональный Документы
Культура Документы
Project #7 Overloading and overriding revisited Interfaces and class inheritance Interface hierarchies
11/29/2006
Inheritance Recap
Inheritance occurs when one class (derived class, subclass) is defined from another class (base / parent class, superclass). To derive a class D from a base class B, use:
public class D extends B { }
Derived class inherits all instance variables, methods from base class. It can also define new instance variables, methods In derived-class constructor, super( ) can be used to invoke constructor from base class Derived class can explicitly refer to entities from base class using super, e.g. super.toString() Polymorphism: object in derived class can be used anywhere base class is expected (a Student is a Person!) Derived class can override base-class methods (and variables)
final can be used to disallow overriding Java uses late binding to determine which version of method to use
Run-time information about class / type information of objects can be obtained using getClass() method and instanceof operator Upcasting of object to superclass type is always safe and done automatically Downcasting may not be safe, depending on actual class object belongs to at run-time. Safe downcasting can be done using instanceof
In presence of inheritance, issues arise in definitions of equals(), copying, because parameter may belong to subclass of argument type (e.g. in Person copy constructor, parameter may be Student rather than Person) For equals(), make argument type Object and check for class membership using getClass() To copy objects, use cloning: embed information about copying in objects rather than classes by implementing clone() method to perform copying Composition is another way to implement one class based on another. It is different from inheritance: an object in a class created with composition has-a instance of an object in the earlier class, as opposed to having an is-a relationship.
CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3
Overriding
A subclass can redefine an existing method from a superclass When overriding a method the subclass method prototype must match exactly the prototype of the superclass (same name, same return type, same arguments). Otherwise: overloading! You may change access specifier (public, private, protected), but derived classes cannot decrease the visibility.
Example
public class Base { protected void m (int x) { } } public class Derived extends Base { public void m (int x) { } public int m (int x) { } public void m (double d) { } }
Overriding: with increased visibility Error! duplicate method declaration Overloading
// The following appears in the same package as above Base b = new Base( ); Base d = new Derived( ); Derived e = new Derived( ); b.m (5); d.m (6); d.m (7.0); e.m (8.0);
calls Derived:someMethod( double)
calls Base:m(int) calls Derived:m(int) Error! Since d is declared Base, this attempts to call Derived:m(int). But argument has wrong type.
5
Is Object Abstract?
No! You can create instance of Object using new
Object o = new Object();
Recall Interfaces
Interfaces contain lists of method prototypes Example from Lecture #23:
public interface Printable { public void print (); public void println (); }
From last time: interfaces are similar to, but different from, abstract classes
Abstract classes can contain abstract, concrete methods Classes can implement multiple interfaces, but inherit (directly) from only one class
Multiple Inheritance?
Intuitively useful to be able to inherit from multiple classes (multiple inheritance)
Person
Student
Athlete
Faculty
StudentAthlete
StudentAthlete
Example
public class Person { } public class Student extends Person { }
public interface Athlete { public String getSport (); public void setSport (String sport); } public class StudentAthlete extends Student implements Athlete { }
Objects in StudentAthlete are Students They also can be wherever objects matching Athlete are required
CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11
static int
public class MonthDemo implements Months { public static void main( String[ ] args ) { System.out.println( "March is month number " + MARCH ); } }
Interface Hierarchies
Inheritance may also be used to build new interfaces from previous ones A subinterface inherits all method / constant declarations from its base interface A subinterface may also introduce new methods / constants E.g. recall Iterator<T> interface
public interface Iterator { boolean hasNext( ); <T> next( ); void remove( ); } // any more items? // return the next item // remove the current item
13