Copyright © Curt Hill Inheritance and Polymorphism A Powerful Technique
Copyright © Curt Hill Biology Example Class is a loan word from biology –Means group of related orders, families and species Animals (a kingdom) –Vertebrates (a phylum) –Mammals (a class) –Rodents (an order) –Squirrels (a family) –Thirteen line ground squirrel (a specie)
Copyright © Curt Hill Relationship The prior list shows an is a relationship A thirteen line ground squirrel is a squirrel A squirrel is a rodent A thirteen line ground has all the characteristics of any of the above
Copyright © Curt Hill Composition and Inheritance Composition expresses a has a relationship –Fraction has a numerator (integer) Inheritance expresses an is a relationship –A student is a person
Copyright © Curt Hill Inheritance Deriving new classes from old classes New class may retain any properties or methods of old New class may add new properties, new methods New class may replace old properties or methods
Copyright © Curt Hill Inheritance example Person has a name and age Student is a person with GPA and major Employee is a person with a wage Grad is a student with degree
Copyright © Curt Hill person employeestudent grad set_name set_wageset_major set_degree
Copyright © Curt Hill Syntax details The Java extends keyword indicates subclass: class student extends person { The person constructor will be called as part of the student constructor If we want a particular constructor from person we call with super
Copyright © Curt Hill Super Constructor Example class student extends person{... public student (String nme, char m_or_f, date d){ super(nme,m_or_f,d); hours=0;... }
Copyright © Curt Hill Properties and methods All classes have properties (eg. name and age) and methods (eg. set_name) –Person explicitly declares it –All the rest inherit it –Can access if public or protected
Copyright © Curt Hill Using superclass methods Use super as a class name to get the immediate super class method super.x(y)
Copyright © Curt Hill The print routine In person: public void print(){ System.out.print(name); System.out.print(' ');... In student: public void print() { super.print(); System.out.print(" hrs: "); System.out.print(hours);
Copyright © Curt Hill Polymorphism Literally: many shapes Classes that are related are interchangable In Java any container, such as an array can have related contents An array of person can also contain students
Copyright © Curt Hill Polymorphism Example person ptr[] = new person[max]; // person must be super class ptr[0] = new person(); ptr[1] = new student(); ptr[2] = new employee();
Copyright © Curt Hill Polymorphic methods What can be done to an array of some super class? Anything that can be done to all of them –Methods defined only in super class Such as get_name –Methods redefined in subclasses Such as print
Copyright © Curt Hill Inherited functions Execute the set_name function against any variable of these four types –The same effect occurs –Defined in the base type –Variable is upcast, that is converted to a base type
Copyright © Curt Hill Polymorphism Execute the print function against any variable of these four types –The different effects occur based on the actual type of the variable –It just calls print knowing that all person derived classes have a print function A function can accept a person type and receive either a person, student, employee or grad without problem
Copyright © Curt Hill Polymorphic Example for (int i=0;i<max;i++) ptr[i].print(); Prints them without knowing in advance which are students and which are persons Not knowing which routine to call until runtime is dynamic binding
Copyright © Curt Hill Polymorhism Again Polymorphism requires no effort by the programmer All that needs to be done is point a handle at a method and execute The system determines what the pointer that it has points at From a type perspective it could be what it is declared or any descendent
Copyright © Curt Hill Object Every class is a subclass of Object Thus: class Item {… is the same as: class Item extends Object { … Thus all classes are polymorphic All may use Object methods –Object has no properties
Copyright © Curt Hill Object There are a variety of methods: –equals –getClass –hashcode –notify –toString –wait These are often overridden in descendent classes
Copyright © Curt Hill The Object Hierarchy Since every class is a subclass of Object then Object obarr[]; can hold anything but a primitive –It can hold a wrapper such as Integer These can be determined at run-time Versions of Java prior to 5 had no generics –Polymorphism substituted
Copyright © Curt Hill Containers and Polymorphism An array is not the only container Java also has a variety of container classes such as LinkList, Stack, TreeSet, Vector etc. These are dynamic data structures that may contain objects of certain types The types only have to be polymorphically related
Conclusion Inheritance requires the extends reserved word –Interfaces uses implements Java objects are handle based so always available –The handles are always the same size Object is the root of a single inheritance tree, so a container class could contain any object Copyright © Curt Hill