CMSC 341 Inheritance and the Collection classes Inheritance in Java Inheritance is implemented using the keyword extends public class Employee extends Person Class definition goes here only the implementation for the specialized behavior A class may only inherit from only one superclass If a class is not derived from a super class then it is derived from java lang Object The following two class declarations are equivalent public class Person public class Person extends Object 8 03 2007 UMBC CMSC 341 Java 3 2 Polymorphism If Employee is a class that extends Person an Employee is a Person and polymorphism can occur Creates an array of Person references Person p new Person 2 p 0 new Employee p 1 new Person 8 03 2007 UMBC CMSC 341 Java 3 3 Polymorphism cont However a Person is not necessarily an Employee The following will generate a compile time error Employee e new Person Like C polymorphism requires general class on left of assignment operator and specialized class on right Casting allows you to make such an assignment provided you are confident that it is ok public void convertToPerson Object obj Person p Person obj 8 03 2007 UMBC CMSC 341 Java 3 4 Virtual Method Invocation In Java virtual method invocation is automatic At runtime the JVM determines the actual type of object a reference points to Then the JVM selects the correct overridden method for it Supposing the Employee class overrides the toString method inherited from the Person class then the toString method of the derived class Employee is invoked even though the reference is a Person reference Person p new Employee Invokes the toString method p toString of the Employee class 8 03 2007 UMBC CMSC 341 Java 3 5 What is inherited by the subclass All fields are inherited Giving fields in super classes protected access allows methods of subclasses to reference the fields All methods are inherited except for constructors Inherited methods may be overloaded or overridden 8 03 2007 UMBC CMSC 341 Java 3 6 Constructors and Inheritance The superclass constructors are always called by the constructors of the subclasses either implicitly or explicitly To explicitly call the superclass constructor in the first line of the subclass constructor make a call to the super method passing the appropriate parameters for the desired constructor 8 03 2007 UMBC CMSC 341 Java 3 7 The super Reference All overridden methods in a subclass also contain a reference to their corresponding methods in the superclass named super The following code contains the use of the super reference to call the super class constructor and to use the implementation of the toString method of the superclass Notice it also contains several uses of the this reference 8 03 2007 UMBC CMSC 341 Java 3 8 Super Class Example public class Person protected String name private int age public Person String name int age this name name this age age public Person String name Call to other constructor this name 0 public String toString return name public int getAge return age public void setAge int age this age age public void setName String name this name name 8 03 2007 UMBC CMSC 341 Java 3 9 Subclass Example public class Employee extends Person private double salary public Employee String name int age double sal Call to superclass constructor super name age salary sal public Employee String name double salary Call to constructor above this name 18 salary public double getSalary return salary public void setSalary double sal salary sal public String toString Call to superclass toString method return super toString has a salary of salary 8 03 2007 UMBC CMSC 341 Java 3 10 Polymorphism in Action public class Test public static void main String args Person people new Person 3 people 0 new Person Sam people 1 new Employee Jane 45345 63 for Person someone people System out println someone Output println invokes the toString method of Sam the object the reference is pointing to Jane has a salary of 45345 63 as if it were a pointer in C and the toString method were virtual 8 03 2007 UMBC CMSC 341 Java 3 null 11 Abstract Classes and Methods Java also has abstract classes and methods like C If a class has an abstract method then it must be declared abstract public abstract class Node String public public public Abstract methods have no implementation name abstract void type String toString return name Node String name this name name 8 03 2007 UMBC CMSC 341 Java 3 12 Subclass of Abstract Class Subclass of an abstract class must provide implementation for ALL the abstract methods or it must be declared abstract as well public class NumberNode extends Node int number public void type System out println Number node public NumberNode String name int num super name number num public String toString return super toString number 8 03 2007 UMBC CMSC 341 Java 3 13 More about Abstract Classes Like C abstract classes can not be instantiated OK because n is only a reference Node n OK because NumberNode is concrete Node n new NumberNode Penta 5 Not OK Gives compile error Node n new Node Name 8 03 2007 UMBC CMSC 341 Java 3 14 Multiple Inheritance in Java There are always cases where a class appears to have characteristics of more than one class Consider the following hierarchy Person Student TA 8 03 2007 Employee TA has characteristics of both a Student and an Employee UMBC CMSC 341 Java 3 15 Interfaces Java only allows a class to extend one super class It does not allow multiple inheritance like C However to cope with the need for multiple inheritance it created interfaces An interface is like class without the implementation It contains only public static and final fields and public and abstract method headers no body 8 03 2007 UMBC CMSC 341 Java 3 16 Interface Example A public interface like a public class must be in a file of the same name The methods and fields are implicitly public and abstract by virtue of being declared in an interface public interface Employable void raiseSalary double d double getSalary 8 03 2007 UMBC CMSC 341 Java 3 17 Interfaces cont Many classes may implement the same interface The classes may be in completely different inheritance hierarchies A class may implement several interfaces public class TA extends Student implements Employable Now TA class must implement the getSalary and the raiseSalary methods here 8 03 2007 UMBC CMSC 341 Java 3 18 Inheritance Progression Inheritance of Implementation Point 3DPoint Inheritance of Interface AbstractShape ColorPoint Square Circle
View Full Document