copyright © 1999-2015 curt hill inheritance and polymorphism a powerful technique

24
Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Upload: gilbert-cooper

Post on 05-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 Curt Hill

Inheritance and Polymorphism

A Powerful Technique

Page 2: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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)

Page 3: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 4: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 5: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 6: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 7: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 Curt Hill

person

employee student

grad

set_name

set_wage set_major

set_degree

Page 8: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 9: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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; . . . }

Page 10: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 11: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 Curt Hill

Using superclass methods

• Use super as a class name to get the immediate super class method

• super.x(y)

Page 12: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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);

Page 13: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 14: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 Curt Hill

Polymorphism Example

• person ptr[] = new person[max];// person must be super classptr[0] = new person();ptr[1] = new student();ptr[2] = new employee();

Page 15: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 16: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 17: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 18: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 19: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 20: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 21: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 Curt Hill

Object• There are a variety of methods:

– equals – getClass– hashcode– notify– toString– wait

• These are often overridden in descendent classes

Page 22: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 23: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © 1999-2015 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

Page 24: Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique

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 © 1999-2015 Curt Hill