overview of java’s support for inheritance & polymorphism

49
Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA Overview of Java’s Support for Inheritance

Upload: others

Post on 28-May-2022

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Overview of Java’s Support for Inheritance & Polymorphism

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidtProfessor of Computer Science

Institute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

Overview of Java’s Support for Inheritance

Page 2: Overview of Java’s Support for Inheritance & Polymorphism

2

• Understand what inheritance is & how it’s supported in JavaLearning Objectives in this Lesson

OOP

Abstraction

InheritancePolymorphism

Page 3: Overview of Java’s Support for Inheritance & Polymorphism

3

Overview of Java’s Support for Inheritance

Page 4: Overview of Java’s Support for Inheritance & Polymorphism

4

• OO languages enhance reuse by allowing classes to inherit commonly used state & behavior from other classes

OOP

Abstraction

InheritancePolymorphism

See en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

Overview of Java’s Support for Inheritance

Page 5: Overview of Java’s Support for Inheritance & Polymorphism

5

• Inheritance in Java is specified via its extends keyword

Overview of Java’s Support for Inheritance

See docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

AbstractSetint MAX_ARRAY_SIZE...

boolean hashCode()...

SimpleSetObject[] mElementData...

boolean add(E e)...

<<extends>>

Page 6: Overview of Java’s Support for Inheritance & Polymorphism

6

• Inheritance in Java is specified via its extends keyword• Allows a subclass to inherit all

non-private fields & methodsfrom a super class

Overview of Java’s Support for Inheritance

See docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

AbstractSetint MAX_ARRAY_SIZE...

boolean hashCode()...

SimpleSetObject[] mElementData...

boolean add(E e)...

<<extends>>

Page 7: Overview of Java’s Support for Inheritance & Polymorphism

7

• Inheritance in Java is specified via its extends keyword• Allows a subclass to inherit all

non-private fields & methodsfrom a super class

Overview of Java’s Support for InheritanceAbstractSet

int MAX_ARRAY_SIZE...

boolean hashCode()...

SimpleSetObject[] mElementData...

boolean add(E e)...

<<extends>>

SimpleSet inherits MAX_ARRAY _SIZE & hashCode() (among

others) from AbstractSet

Page 8: Overview of Java’s Support for Inheritance & Polymorphism

8

• Inheritance in Java is specified via its extends keyword• Allows a subclass to inherit all

non-private fields & methodsfrom a super class

Overview of Java’s Support for InheritanceAbstractSet

int MAX_ARRAY_SIZE...

boolean hashCode()...

SimpleSetObject[] mElementData...

boolean add(E e)...

<<extends>>Set<String> s = new SimpleSet();

s.hashCode();

s.add(element);

Page 9: Overview of Java’s Support for Inheritance & Polymorphism

9

• Inheritance in Java is specified via its extends keyword• Allows a subclass to inherit all

non-private fields & methodsfrom a super class

Overview of Java’s Support for InheritanceAbstractSet

int MAX_ARRAY_SIZE...

boolean hashCode()...

SimpleSetObject[] mElementData...

boolean add(E e)...

<<extends>>Set<String> s = new SimpleSet();

s.hashCode();

s.add(element);

Page 10: Overview of Java’s Support for Inheritance & Polymorphism

10

• Inheritance in Java is specified via its extends keyword• Allows a subclass to inherit all

non-private fields & methodsfrom a super class

Overview of Java’s Support for InheritanceAbstractSet

int MAX_ARRAY_SIZE...

boolean hashCode()...

SimpleSetObject[] mElementData...

boolean add(E e)...

<<extends>>Set<String> s = new SimpleSet();

s.hashCode();

s.add(element);

Page 11: Overview of Java’s Support for Inheritance & Polymorphism

11

• Inheritance in Java is specified via its extends keyword• Allows a subclass to inherit all

non-private fields & methodsfrom a super class

Overview of Java’s Support for InheritanceAbstractSet

int MAX_ARRAY_SIZE...

boolean hashCode()...

SimpleSetObject[] mElementData...

boolean add(E e)boolean hashCode()

<<extends>>Set<String> s = new SimpleSet();

s.hashCode();

s.add(element);

SimpleSet can also “override” hashCode(), as discussed in Polymorphism lesson

Page 12: Overview of Java’s Support for Inheritance & Polymorphism

12

• Java Collections Framework demonstrates capabilities & benefits of inheritanceOverview of Java’s Support for Inheritance

See docs.oracle.com/javase/8/docs/technotes/guides/collections

These classes & interfaces are

organized hierarchically

Page 13: Overview of Java’s Support for Inheritance & Polymorphism

13

• Java Collections Framework demonstrates capabilities & benefits of inheritanceOverview of Java’s Support for Inheritance

See the lesson on “Overview of the Java Collections Framework”

Page 14: Overview of Java’s Support for Inheritance & Polymorphism

14See docs.oracle.com/javase/8/docs/api/java/util/List.html

Overview of Java’s Support for Inheritance• The List hierarchy are collections

that maintain an ordering fortheir elements

Page 15: Overview of Java’s Support for Inheritance & Polymorphism

15See developer.android.com/reference/java/util/List.html

Overview of Java’s Support for Inheritance

See docs.oracle.com/javase/8/docs/api/java/util/AbstractCollection.html

• The List hierarchy are collectionsthat maintain an ordering fortheir elements

Page 16: Overview of Java’s Support for Inheritance & Polymorphism

16See developer.android.com/reference/java/util/List.html

Overview of Java’s Support for Inheritance

See docs.oracle.com/javase/8/docs/api/java/util/AbstractList.html

• The List hierarchy are collectionsthat maintain an ordering fortheir elements

Page 17: Overview of Java’s Support for Inheritance & Polymorphism

17See developer.android.com/reference/java/util/List.html

Overview of Java’s Support for Inheritance

See docs.oracle.com/javase/8/docs/api/java/util/Vector.html

• The List hierarchy are collectionsthat maintain an ordering fortheir elements

Page 18: Overview of Java’s Support for Inheritance & Polymorphism

18See docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Overview of Java’s Support for Inheritance• The List hierarchy are collections

that maintain an ordering fortheir elements

Page 19: Overview of Java’s Support for Inheritance & Polymorphism

19See developer.android.com/reference/java/util/List.html

Overview of Java’s Support for Inheritance

This inheritance hierarchy enhances systematic reuse of data fields & methods

• The List hierarchy are collectionsthat maintain an ordering fortheir elements

Page 20: Overview of Java’s Support for Inheritance & Polymorphism

20

The Role of the Java Object Super Class

Page 21: Overview of Java’s Support for Inheritance & Polymorphism

21

• All Java classes inherit from the java.lang.Object super class

The Role of the Java Object Super Class

See docs.oracle.com/javase/8/docs/api/java/lang/Object.html

package java.lang;inpublic class Object {

...public int hashCode();public boolean equals

(Object o);...public final void wait();public final void notify();public final void notifyAll();...

Page 22: Overview of Java’s Support for Inheritance & Polymorphism

22

• All Java classes inherit from the java.lang.Object super class• Defines methods that can be

used by all non-primitive types

The Role of the Java Object Super Classpackage java.lang;inpublic class Object {

...public int hashCode();public boolean equals

(Object o);...public final void wait();public final void notify();public final void notifyAll();...

See docs.oracle.com/javase/8/docs/api/java/lang/Object.html

Page 23: Overview of Java’s Support for Inheritance & Polymorphism

23

• All Java classes inherit from the java.lang.Object super class• Defines methods that can be

used by all non-primitive types

The Role of the Java Object Super Classpackage java.lang;inpublic class Object {

...public int hashCode();public boolean equals

(Object o);...public final void wait();public final void notify();public final void notifyAll();...

See docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode

Page 24: Overview of Java’s Support for Inheritance & Polymorphism

24

• All Java classes inherit from the java.lang.Object super class• Defines methods that can be

used by all non-primitive types

The Role of the Java Object Super Classpackage java.lang;inpublic class Object {

...public int hashCode();public boolean equals

(Object o);...public final void wait();public final void notify();public final void notifyAll();...

See docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals

Page 25: Overview of Java’s Support for Inheritance & Polymorphism

25

• All Java classes inherit from the java.lang.Object super class• Defines methods that can be

used by all non-primitive types

The Role of the Java Object Super Classpackage java.lang;inpublic class Object {

...public int hashCode();public boolean equals

(Object o);...public final void wait();public final void notify();public final void notifyAll();...

See docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

Page 26: Overview of Java’s Support for Inheritance & Polymorphism

26

• Subclasses that don’t explicitly extend a super class implicitly inherit from java.lang.Object

The Role of the Java Object Super Classpackage java.lang;inpublic abstract class Process {

...public abstract int waitFor() ...;

...

Page 27: Overview of Java’s Support for Inheritance & Polymorphism

27

• Subclasses that don’t explicitly extend a super class implicitly inherit from java.lang.Object, e.g.• java.lang.Process implicitly

extends java.lang.Object

The Role of the Java Object Super Classpackage java.lang;inpublic abstract class Process {

...public abstract int waitFor() ...;

...

See docs.oracle.com/javase/8/docs/api/java/lang/Process.html

Page 28: Overview of Java’s Support for Inheritance & Polymorphism

28

• Subclasses that don’t explicitly extend a super class implicitly inherit from java.lang.Object, e.g.• java.lang.Process implicitly

extends java.lang.Object• All instances of java.lang.Process

therefore also provide clients accessto inherited java.lang.Object methods• All objects, including arrays, implement

the methods of this class

The Role of the Java Object Super ClassObject

hashCode();equals();wait()notify();notifyAll();…

Process

waitFor()…

Page 29: Overview of Java’s Support for Inheritance & Polymorphism

29

• java.lang.Object is the most general of all classes

The Role of the Java Object Super Class

Object

Page 30: Overview of Java’s Support for Inheritance & Polymorphism

30

• java.lang.Object is the most general of all classes• It serves as the root of a

hierarchy of classes availableto Java apps

The Role of the Java Object Super Class

… …

… …

……

Object

Page 31: Overview of Java’s Support for Inheritance & Polymorphism

31

• java.lang.Object is the most general of all classes• It serves as the root of a

hierarchy of classes availableto Java apps

• Classes towards the bottom of the inheritance hierarchy are more specialized

The Role of the Java Object Super Class

… …

… …

……

Object

Page 32: Overview of Java’s Support for Inheritance & Polymorphism

32

• java.lang.Object is the most general of all classes• It serves as the root of a

hierarchy of classes availableto Java apps

• Classes towards the bottom of the inheritance hierarchy are more specialized• e.g., List-related subclasses

override methods inheritedfrom super classes

The Role of the Java Object Super Class

AbstractList …

Vector

LinkedList …

ArrayList

…AbstractCollection

Object

Page 33: Overview of Java’s Support for Inheritance & Polymorphism

33

The Role of the Java Object Super Class• java.lang.Object defines.equals()

• If operator== is used to compare the equality of two objects it returns true if the two objects have the same memory address

Object

wait()notify();notifyAll();equals();…

Process

waitFor()…

Page 34: Overview of Java’s Support for Inheritance & Polymorphism

34

The Role of the Java Object Super Class• java.lang.Object defines.equals()

• If operator== is used to compare the equality of two objects it returns true if the two objects have the same memory address

• Conversely, if you use .equals() to compare for equality, a subclass can override this method to do other things• e.g., check for equal values in

a collection or string

Object

wait()notify();notifyAll();equals();…

Process

waitFor()…

Page 35: Overview of Java’s Support for Inheritance & Polymorphism

35

The Three Purposes of Subclass Methods

Page 36: Overview of Java’s Support for Inheritance & Polymorphism

36

• Subclass methods inherited from a super class are used for 3 purposes

public class Stack<E> {extends Vector<E> {

The Three Purposes of Subclass Methods

Page 37: Overview of Java’s Support for Inheritance & Polymorphism

37

• Subclass methods inherited from a super class are used for 3 purposes

public class Stack<E> {extends Vector<E> {

The Three Purposes of Subclass Methods

See docs.oracle.com/javase/8/docs/api/java/util/Stack.html

Extends Vector to define a last-in/first-out data structure that enables apps to pop & push items to/from a stack

Page 38: Overview of Java’s Support for Inheritance & Polymorphism

38

• Subclass methods inherited from a super class are used for 3 purposes

public class Stack<E> {extends Vector<E> {

The Three Purposes of Subclass Methods

See docs.oracle.com/javase/8/docs/api/java/util/Vector.html

Extends Vector to define a last-in/first-out data structure that enables apps to pop & push items to/from a stack

Page 39: Overview of Java’s Support for Inheritance & Polymorphism

39

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API

public class Stack<E> {extends Vector<E> {

The Three Purposes of Subclass Methods

Page 40: Overview of Java’s Support for Inheritance & Polymorphism

40

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API

• e.g., Stack subclass inherits fields & methods from Vector super class

public class Stack<E> {extends Vector<E> {

The Three Purposes of Subclass Methods

Page 41: Overview of Java’s Support for Inheritance & Polymorphism

41

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API

• e.g., Stack subclass inherits fields & methods from Vector super class

public class Stack<E> {extends Vector<E> {

e.g.,

Stack<Integer> s = new Stack<>();

...

if(!s.isEmpty())s.pop();

isEmpty() method inherited from Vector can be invoked on a Stack instance

The Three Purposes of Subclass Methods

This Stack class uses so-called “implementation inheritance”

Page 42: Overview of Java’s Support for Inheritance & Polymorphism

42

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API

• e.g., Stack subclass inherits fields & methods from Vector super class

public class Stack<E> {extends Vector<E> {

e.g.,

Stack<Integer> s = new Stack<>();

...

if(!s.isEmpty())s.pop();

A method can be invoked on an object instance

The Three Purposes of Subclass Methods

Page 43: Overview of Java’s Support for Inheritance & Polymorphism

43

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API 2. To implement subclass methods

public class Stack<E> {extends Vector<E> {

...public Object push(E e){addElement(e);return e;

}...

The Three Purposes of Subclass Methods

Due to implementation inheritance methods like addElement() are visible to Stack clients..

Page 44: Overview of Java’s Support for Inheritance & Polymorphism

44

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API 2. To implement subclass methods

• A subclass may add new methods & data members

public class Stack<E> {extends Vector<E> {

...public Object push(E e){addElement(e);return e;

}...

The Three Purposes of Subclass Methods

The Stack’s push() method is implemented via Vector’s

addElement() method

Page 45: Overview of Java’s Support for Inheritance & Polymorphism

45

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API 2. To implement subclass methods3. To override super class methods

in subclass with same signatures

public abstract class AbstractMap<K,V> ...

public abstract Set<Entry<K,V>> entrySet();

...

public HashMap<K,V> extendsAbstractMap<K,V> ...

public Set<Entry<K,V>>entrySet() { ... }

...

The Three Purposes of Subclass Methods

See en.wikipedia.org/wiki/Type_signature#Java_2

HashMap’sHashMap’s entrySet() method overrides AbstractMap’s entrySet() abstract method

Page 46: Overview of Java’s Support for Inheritance & Polymorphism

46

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API 2. To implement subclass methods3. To override super class methods

in subclass with same signatures• A subclass may define methods

with the same signatures as super class methods

The Three Purposes of Subclass Methodspublic abstract class

AbstractMap<K,V> ... public abstract Set<Entry<K,V>> entrySet();

...

public HashMap<K,V> extendsAbstractMap<K,V> ...

public Set<Entry<K,V>>entrySet() { ... }

...

See docs.oracle.com/javase/tutorial/java/IandI/override.html

HashMap’sHashMap’s entrySet() method overrides AbstractMap’s entrySet() abstract method

Page 47: Overview of Java’s Support for Inheritance & Polymorphism

47

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API 2. To implement subclass methods3. To override super class methods

in subclass with same signatures• A subclass may define methods

with the same signatures as super class methods

• These methods “override” the original methods

The Three Purposes of Subclass Methodspublic abstract class

AbstractMap<K,V> ... public abstract Set<Entry<K,V>> entrySet();

...

public HashMap<K,V> extendsAbstractMap<K,V> ...

public Set<Entry<K,V>>entrySet() { ... }

...

HashMap’sHashMap’s entrySet() method overrides AbstractMap’s entrySet() abstract method

See en.wikipedia.org/wiki/Method_overriding

Page 48: Overview of Java’s Support for Inheritance & Polymorphism

48

• Subclass methods inherited from a super class are used for 3 purposes1. To augment the subclass API 2. To implement subclass methods3. To override super class methods

in subclass with same signatures• A subclass may define methods

with the same signatures as super class methods

• These methods “override” the original methods

The Three Purposes of Subclass Methods

Method overriding is covered in the next lesson on Java Polymorphism

public abstract class AbstractMap<K,V> ...

public abstract Set<Entry<K,V>> entrySet();

...

public HashMap<K,V> extendsAbstractMap<K,V> ...

public Set<Entry<K,V>>entrySet() { ... }

...

HashMap’sHashMap’s entrySet() method overrides AbstractMap’s entrySet() abstract method

Page 49: Overview of Java’s Support for Inheritance & Polymorphism

49

End of Overview of Java’s Support for Inheritance