Académique Documents
Professionnel Documents
Culture Documents
Programming Language
Overview of the Java
Programming Language
Abstraction
OOP
Polymorphism Inheritance
See www.stroustrup.com/whatis.pdf
Overview of the Java
Programming Language
Abstraction
OOP
Polymorphism Inheritance
OOP
Polymorphism Inheritance
See www.stroustrup.com/whatis.pdf
Overview of the Java
Programming Language
Abstraction
Polymorphism Inheritance
See www.stroustrup.com/whatis.pdf
Overview of the Java
Programming Language
Abstraction
Polymorphism Inheritance
See www.stroustrup.com/whatis.pdf
Overview of the Java
Programming Language
Abstraction
Polymorphism Inheritance
See www.stroustrup.com/whatis.pdf
Overview of the Java
Programming Language
Learning Objectives
• Understand what these
advanced object-oriented
(OO) concepts mean
• Know the benefits they
provide developers of Java
apps in Android
• Identify Java features that
implement these OO
concepts
Overview of the Java
Programming Language
Learning Objectives
• Understand what these
advanced object-oriented
(OO) concepts mean
• Know the benefits they
provide developers of Java
apps in Android
• Identify Java features that
implement these OO
concepts
Overview of the Java
Programming Language
Learning Objectives
• Understand what these
advanced object-oriented
(OO) concepts mean
• Know the benefits they
provide developers of Java
apps in Android
• Identify Java features that
implement these OO
concepts
Overview of the Java
Programming Language
Learning Objectives
• Understand what these
advanced object-oriented
(OO) concepts mean
• Know the benefits they
provide developers of Java
apps in Android
• Identify Java features that
implement these OO
concepts
Overview of the Java
Programming Language
Overview of the Java
Programming Language
1
Overview of Java’s Support for Inheritance
• OO languages enhance reuse by allowing classes to inherit commonly used
state & behavior from other classes
Abstraction
OOP
Polymorphism Inheritance
See en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
2
Overview of Java’s Support for Inheritance
• Inheritance in Java is specified via its ClassA
extends keyword Class1 mField1
...
void foo()
...
<<extends>>
ClassB
Class2 mFieldA
...
void bar()
...
See docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
3
Overview of Java’s Support for Inheritance
• Inheritance in Java is specified via its ClassA
extends keyword Class1 mField1
• Allows a subclass to inherit all ...
non-private fields & methods void foo()
from a super class ...
<<extends>>
ClassB
Class2 mFieldA
...
void bar()
...
See docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
4
Overview of Java’s Support for Inheritance
• Inheritance in Java is specified via its ClassA
extends keyword Class1 mField1
• Allows a subclass to inherit all ...
non-private fields & methods void foo()
from a super class ...
<<extends>>
ClassB
ClassB inherits mField1 Class2 mFieldA
& foo() from ClassA ...
void bar()
...
5
Overview of Java’s Support for Inheritance
• Inheritance in Java is specified via its ClassA
extends keyword Class1 mField1
• Allows a subclass to inherit all ...
non-private fields & methods void foo()
from a super class ...
<<extends>>
ClassB
ClassB inherits mField1 Class2 mFieldA
& foo() from ClassA ...
void bar()
...
6
Overview of Java’s Support for Inheritance
• Inheritance in Java is specified via its ClassA
extends keyword Class1 mField1
• Allows a subclass to inherit all ...
non-private fields & methods void foo()
from a super class ...
<<extends>>
ClassB
ClassB inherits mField1 Class2 mFieldA
& foo() from ClassA ...
void bar()
...
7
Overview of Java’s Support for Inheritance
• Inheritance in Java is specified via its ClassA
extends keyword Class1 mField1
• Allows a subclass to inherit all ...
non-private fields & methods void foo()
from a super class ...
<<extends>>
Class B b = new ClassB();
ClassB
b.foo(); Class2 mFieldA
b.bar(); ...
void bar()
...
8
Overview of Java’s Support for Inheritance
• Inheritance in Java is specified via its ClassA
extends keyword Class1 mField1
• Allows a subclass to inherit all ...
non-private fields & methods void foo()
from a super class ...
<<extends>>
Class B b = new ClassB();
ClassB
b.foo(); Class2 mFieldA
b.bar(); ...
void bar()
...
9
Overview of Java’s Support for Inheritance
• Inheritance in Java is specified via its ClassA
extends keyword Class1 mField1
• Allows a subclass to inherit all ...
non-private fields & methods void foo()
from a super class ...
<<extends>>
Class B b = new ClassB();
ClassB
b.foo(); Class2 mFieldA
b.bar(); ...
void bar()
...
10
Overview of Java’s Support for Inheritance
• Java Collections Framework demonstrates capabilities & benefits of inheritance
See docs.oracle.com/javase/8/docs/technotes/guides/collections
11
Overview of Java’s Support for Inheritance
• Java Collections Framework demonstrates capabilities & benefits of inheritance
See docs.oracle.com/javase/8/docs/technotes/guides/collections
12
Overview of Java’s Support for Inheritance
• Java Collections Framework demonstrates capabilities & benefits of inheritance
See developer.android.com/reference/java/util/List.html
14
Overview of Java’s Support for Inheritance
• The List hierarchy are collections
that maintain an ordering for
their elements
See developer.android.com/reference/java/util/AbstractCollection.html
See developer.android.com/reference/java/util/List.html
15
Overview of Java’s Support for Inheritance
• The List hierarchy are collections
that maintain an ordering for
their elements
See See
developer.android.com/reference/java/util/AbstractList.html
developer.android.com/reference/java/util/List.html
16
Overview of Java’s Support for Inheritance
• The List hierarchy are collections
that maintain an ordering for
their elements
See
Seedeveloper.android.com/reference/java/util/Vector.html
developer.android.com/reference/java/util/List.html
17
Overview of Java’s Support for Inheritance
• The List hierarchy are collections
that maintain an ordering for
their elements
See developer.android.com/reference/java/util/ArrayList.html
18
Overview of Java’s Support for Inheritance
• The List hierarchy are collections
that maintain an ordering for
their elements
See developer.android.com/reference/java/util/List.html
This inheritance hierarchy enhances systematic
19 reuse of data fields & methods
Overview of Java’s
Support for
Inheritance (Part 2)
20
Overview of Java’s Support for Inheritance
• All Java classes inherit from the package java.lang;
java.lang.Object super class in
public class Object {
...
public int hashCode();
public boolean equals
(Object o);
...
public final void wait();
public final void notify();
public final void notifyAll();
...
See developer.android.com/reference/java/lang/Object.html
21
Overview of Java’s Support for Inheritance
• All Java classes inherit from the package java.lang;
java.lang.Object super class in
public class Object {
• Defines methods that can be ...
used by all non-primitive types public int hashCode();
public boolean equals
(Object o);
...
public final void wait();
public final void notify();
public final void notifyAll();
...
See developer.android.com/reference/java/lang/Object.html
22
Overview of Java’s Support for Inheritance
• All Java classes inherit from the package java.lang;
java.lang.Object super class in
public class Object {
• Defines methods that can be ...
used by all non-primitive types public int hashCode();
public boolean equals
(Object o);
...
public final void wait();
public final void notify();
public final void notifyAll();
...
See developer.android.com/reference/java/lang/Object.html#hashCode()
23
Overview of Java’s Support for Inheritance
• All Java classes inherit from the package java.lang;
java.lang.Object super class in
public class Object {
• Defines methods that can be ...
used by all non-primitive types public int hashCode();
public boolean equals
(Object o);
...
public final void wait();
public final void notify();
public final void notifyAll();
...
See developer.android.com/reference/java/lang/Object.html#equals(java.lang.Object)
24
Overview of Java’s Support for Inheritance
• All Java classes inherit from the package java.lang;
java.lang.Object super class in
public class Object {
• Defines methods that can be ...
used by all non-primitive types 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
25
Overview of Java’s Support for Inheritance
• Subclasses that don’t explicitly package java.lang;
extend a super class implicitly in
inherit from java.lang.Object public abstract class Process {
...
public abstract int waitFor()
...;
...
26
Overview of Java’s Support for Inheritance
• Subclasses that don’t explicitly package java.lang;
extend a super class implicitly in
inherit from java.lang.Object, e.g. public abstract class Process {
...
• java.lang.Process implicitly public abstract int waitFor()
extends java.lang.Object ...;
...
See developer.android.com/reference/java/lang/Process.html
27
Overview of Java’s Support for Inheritance
• Subclasses that don’t explicitly Object
extend a super class implicitly
inherit from java.lang.Object, e.g. hashCode();
equals();
• java.lang.Process implicitly wait()
extends java.lang.Object notify();
• All instances of java.lang.Process notifyAll();
therefore also provide clients access …
to inherited java.lang.Object methods
Process
waitFor()
…
28
Overview of Java’s Support for Inheritance
• java.lang.Object is the most general
of all classes Object
29
Overview of Java’s Support for Inheritance
• java.lang.Object is the most general
of all classes Object
• It serves as the root of a
hierarchy of classes available … …
to Java apps
… …
… …
… …
30
Overview of Java’s Support for Inheritance
• java.lang.Object is the most general
of all classes Object
• It serves as the root of a
hierarchy of classes available … …
to Java apps
• Classes towards the bottom of
the inheritance hierarchy are … …
more specialized
… …
… …
31
Overview of Java’s Support for Inheritance
• java.lang.Object is the most general
of all classes Object
• It serves as the root of a
hierarchy of classes available Abstract
…
to Java apps Collection
32
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
33
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
See developer.android.com/reference/java/util/Stack.html
34
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
See developer.android.com/reference/java/util/Vector.html
35
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
1. To augment the subclass API
36
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
1. To augment the subclass API
• e.g., Stack subclass inherits fields
& methods from Vector super class
37
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
1. To augment the subclass API
e.g.,
• e.g., Stack subclass inherits fields
& methods from Vector super class Stack<Integer> s =
new Stack<>();
...
if(!s.isEmpty())
s.pop();
38
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
...
1. To augment the subclass API
public Object push(E e){
2. To implement subclass methods addElement(e);
return e;
}
...
39
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
...
1. To augment the subclass API
public Object push(E e){
2. To implement subclass methods addElement(e);
• e.g., Stack’s push() method is return e;
implemented via addElement() }
...
method inherited from Vector
40
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public class Stack<E> {
super class are used for 3 purposes extends Vector<E> {
...
1. To augment the subclass API
public Object push(E e){
2. To implement subclass methods addElement(e);
• e.g., Stack’s push() method is return e;
implemented via addElement() }
...
method inherited from Vector
41
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public abstract class
super class are used for 3 purposes AbstractMap<K,V> ...
public abstract
1. To augment the subclass API
Set<Entry<K,V>> entrySet();
2. To implement subclass methods public V put(K key, V value)
3. To override super class methods { ... }
in subclass with same signatures ...
See en.wikipedia.org/wiki/Type_signature#Java_2
42
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public abstract class
super class are used for 3 purposes AbstractMap<K,V> ...
public abstract
1. To augment the subclass API
Set<Entry<K,V>> entrySet();
2. To implement subclass methods public V put(K key, V value)
3. To override super class methods { ... }
in subclass with same signatures ...
See docs.oracle.com/javase/tutorial/java/IandI/override.html
43
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public abstract class
super class are used for 3 purposes AbstractMap<K,V> ...
public abstract
1. To augment the subclass API
Set<Entry<K,V>> entrySet();
2. To implement subclass methods public V put(K key, V value)
3. To override super class methods { ... }
in subclass with same signatures ...
See docs.oracle.com/javase/tutorial/java/IandI/override.html
44
Overview of Java’s Support for Inheritance
• Subclass methods inherited from a public abstract class
super class are used for 3 purposes AbstractMap<K,V> ...
public abstract
1. To augment the subclass API
Set<Entry<K,V>> entrySet();
2. To implement subclass methods public V put(K key, V value)
3. To override super class methods { ... }
in subclass with same signatures ...
46
Overview of Java’s Support for Polymorphism
• Inheritance is nearly always used in
conjunction with polymorphism
Abstraction
OOP
Polymorphism Inheritance
47
Overview of Java’s Support for Polymorphism
• Inheritance is nearly always used in
conjunction with polymorphism
Abstraction
Polymorphism Inheritance
48
Overview of Java’s Support for Polymorphism
• Polymorphism enables transparent
customization of methods inherited
Abstraction
from a super class
OOP
Polymorphism Inheritance
See en.wikipedia.org/wiki/Polymorphism_(computer_science)
49
Overview of Java’s Support for Polymorphism
• Polymorphism & inheritance are essential to the “open/closed principle”
See en.wikipedia.org/wiki/Open/closed_principle
50
Overview of Java’s Support for Polymorphism
• Polymorphism & inheritance are essential to the “open/closed principle”
• “A class should be open for extension,
but closed for modification”
See en.wikipedia.org/wiki/Open/closed_principle
51
Overview of Java’s Support for Polymorphism
• Polymorphism & inheritance are essential to the “open/closed principle”
• “A class should be open for extension,
but closed for modification”
• Insulating a class from modifications
helps make the class more robust,
flexible, & reusable
See www.dre.vanderbilt.edu/~schmidt/OCP.pdf
52
Overview of Java’s Support for Polymorphism
• The “open/closed principle” can be applied in conjunction with patterns to
enable extensions without modifying existing classes or apps
See en.wikipedia.org/wiki/Factory_method_pattern
53
Overview of Java’s Support for Polymorphism
• The “open/closed principle” can be applied in conjunction with patterns to
enable extensions without modifying existing classes or apps
See en.wikipedia.org/wiki/Factory_method_pattern
54
Overview of Java’s Support for Polymorphism
• The “open/closed principle” can be applied in conjunction with patterns to
enable extensions without modifying existing classes or apps
See en.wikipedia.org/wiki/Factory_method_pattern
55
Overview of Java’s Support for Polymorphism
• The “open/closed principle” can be applied in conjunction with patterns to
enable extensions without modifying existing classes or apps
See en.wikipedia.org/wiki/Factory_method_pattern
56
Overview of Java’s Support for Polymorphism
• The “open/closed principle” can be applied in conjunction with patterns to
enable extensions without modifying existing classes or apps
See en.wikipedia.org/wiki/Factory_method_pattern
57
Overview of Java’s Support for Polymorphism
• The “open/closed principle” can be applied in conjunction with patterns to
enable extensions without modifying existing classes or apps
See en.wikipedia.org/wiki/Factory_method_pattern
58
Overview of Java’s Support for Polymorphism
• Subclasses defined in accordance with the open/closed
principle can define their own custom behaviors that
are more suitable for a particular use case
<<extends>> <<extends>>
59
Overview of Java’s Support for Polymorphism
• Subclasses defined in accordance with the open/closed
principle can define their own custom behaviors that
are more suitable for a particular use case
• While still reusing structure
& functionality from super class
<<extends>> <<extends>>
60
Overview of Java’s Support for Polymorphism
• Subclasses defined in accordance with the open/closed
principle can define their own custom behaviors that
are more suitable for a particular use case
• While still reusing structure
& functionality from super class
• e.g., Vector & ArrayList both
inherit AbstractList methods
<<extends>> <<extends>>
61
Overview of Java’s Support for Polymorphism
• Subclasses defined in accordance with the open/closed
principle can define their own custom behaviors that
are more suitable for a particular use case
• While still reusing structure
& functionality from super class
• e.g., Vector & ArrayList both
inherit AbstractList methods
<<extends>> <<extends>>
62
Overview of Java’s
Support for
Polymorphism (Part 2)
63
Overview of Java’s Support for Polymorphism
• Subclasses defined in accordance with the open/closed
principle can define their own custom behaviors that
are more suitable for a particular use case
• While still reusing structure
& functionality from super class
• e.g., Vector & ArrayList both
inherit AbstractList methods
<<extends>> <<extends>>
See beginnersbook.com/2013/12/difference-between-arraylist-and-vector-in-java
64
Overview of Java’s Support for Polymorphism
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
entrySet()
put()
65
Overview of Java’s Support for Polymorphism
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
entrySet()
• Subclass methods can override
put()
super class methods
66
Overview of Java’s Support for Polymorphism
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
entrySet()
• Subclass methods can override
put()
super class methods
67
Overview of Java’s Support for Polymorphism
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
entrySet()
• Subclass methods can override
put()
super class methods
68
Overview of Java’s Support for Polymorphism
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
entrySet()
• Subclass methods can override
put()
super class methods
69
Overview of Java’s Support for Polymorphism
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
entrySet()
• Subclass methods can override
put()
super class methods
70
Overview of Java’s Support for Polymorphism
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
entrySet()
• Subclass methods can override
put()
super class methods
See developer.android.com/reference/java/util/HashMap.html
72
Overview of Java’s Support for Polymorphism
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
entrySet()
• Subclass methods can override
put()
super class methods
See developer.android.com/reference/java/util/TreeMap.html
73
Overview of Java’s Support for Polymorphism
// Create AbstractMap subclass
AbstractMap<K,V>
AbstractMap<Key, Value>
absMap = makeMap(mapType); …
... entrySet()
// Dispatch put() method put()
absMap.put(key, value);
74
Overview of Java’s Support for Polymorphism
// Create AbstractMap subclass
AbstractMap<K,V>
AbstractMap<Key, Value>
absMap = makeMap(mapType); …
... entrySet()
// Dispatch put() method put()
absMap.put(key, value);
See en.wikipedia.org/wiki/Factory_method_pattern
78 & en.wikipedia.org/wiki/Open/closed_principle
Overview of Java’s Support for Polymorphism
// Create AbstractMap subclass
AbstractMap<K,V>
AbstractMap<Key, Value>
absMap = makeMap(mapType); …
... entrySet()
// Dispatch put() method put()
absMap.put(key, value);
See en.wikipedia.org/wiki/Factory_method_pattern
79 & en.wikipedia.org/wiki/Open/closed_principle
Overview of the Java
Programming Language
Abstraction
OOP
Polymorphism Inheritance
Overview of the Java
Programming Language
Overview of the Java
Programming Language
Overview of the Java
Programming Language
Learning Objectives
• Understand the key object-
oriented (OO) concepts
supported by Java
• Know the benefits that OO
concepts provide developers
of Java apps in Android
• Identify Java features that
implement these OO
concepts
Overview of the Java
Programming Language
Learning Objectives
• Understand the key object-
oriented (OO) concepts
supported by Java
• Know the benefits that OO
concepts provide developers
of Java apps in Android
• Identify Java features that
implement these OO
concepts
Overview of the Java
Programming Language
Learning Objectives
• Understand the key object-
oriented (OO) concepts
supported by Java
• Know the benefits that OO
concepts provide developers
of Java apps in Android
• Identify Java features that
implement these OO
concepts
Overview of the Java
Programming Language
Learning Objectives
• Understand the key object-
oriented (OO) concepts
supported by Java
• Know the benefits that OO
concepts provide developers
of Java apps in Android
• Identify Java features that
implement these OO
concepts
Overview of the Java
Programming Language
Overview of the Java
Programming Language
See download.oracle.com/javase/tutorial
Overview of the Java
Programming Language
See developer.android.com/
reference/packages.html
Overview of the Java
Programming Language