MOBILE APPLICATION
DEVELOPMENT
JAVA Review
Speaker: Luu Quang Huan, MsC
Faculty of Computer Science and Engineering
Ho Chi Minh University of Technology, VNU-HCM
9/23/20 Mobile Application Development
2
• If you already known Java you may be bored by some parts of this lesson!
• You can move quickly through this material to prepare for the next lesson
9/23/20 Mobile Application Development
3
Copyright Notice
• This slide based on the material on
• “Overview of the Java Programming Language” on the Coursera.
• https://www.geeksforgeeks.org/
9/23/20 Mobile Application Development
4
Learning Object
1. Identify Java features that implement these OO concepts
2. Java Collection framework
3. Key Object-Oriented Concepts Supported by Java
1. Abstraction Understand what these advanced
2. Polymorphism object-oriented (OO) concepts
3. Inheritance mean
4. Some pattern design with Java
5. Know the benefits they provide developers of Java apps in Android
9/23/20 Mobile Application Development
5
JAVA OVERVIEW
9/23/20 Mobile Application Development
6
Key Object-Oriented Concepts Supported by
Java
• Java is an object-oriented programming language
9/23/20 Mobile Application Development
7
• Apps written in Java are organized in
terms of structural elements
9/23/20 Mobile Application Development
8
• Apps written in Java are organized in terms of structural elements
• e.g., classes, interfaces, & packages
9/23/20 Mobile Application Development
9
• An object is an instance of a class anObject : SomeClass
that performs certain operations
& interacts with other objects Class1 mField1
• An object in Java resides in a Class2 mField2
...
memory location of a computer
• It consists of void method1()
• State – represented via data fields void method2()
void method3()
• Behavior – represented via ...
methods
9/23/20 Mobile Application Development
13
• Objects often correspond to real-world anAccount : Account
entities
Money mCurrentBalance
boolean mOverdraftProtection
...
void deposit(Money amount)
void withdrawl(Money amount)
Money checkCurrentBalance()
...
9/23/20 Mobile Application Development
18
• Non-object-oriented programming languages organize
apps in terms of functional elements
• e.g., actions & logic
9/23/20 Mobile Application Development
19
• Object-oriented Java programs also Account
perform actions & contain logic
Money mCurrentBalance
boolean mOverdraftProtection
...
• void deposit(Money amount) void
withdrawl(Money amount) Money
checkCurrentBalance()
• ...
9/23/20 Mobile Application Development
20
• Object-oriented Java programs also
perform actions & contain logic
• However, these functional elements
don’t constitute main focus in Java
9/23/20 Mobile Application Development
71
• Classes & interfaces can be java
grouped into packages, e.g.
io
• java.lang contains classes lang util
fundamental to design of Java Integer ArrayList File
String HashMap FileInputStream
• java.util contains a collection Thread Vector FileOutputStream
… … …
of common reusable ADTs
• java.io contains classes that
provide operations on files
9/23/20 Mobile Application Development
15
• Classes & interfaces can be java
grouped into packages
lang util io
Integer ArrayList File
String HashMap FileInputStream
Thread Vector FileOutputStream
… … …
9/23/20 Mobile Application Development
16
• Java generics enable ADTs to be
parameters when defining classes,
interfaces, & methods
9/23/20 Mobile Application Development
81
• Generics offer several benefits to package java.util;
Java programmers, e.g.
public class Vector<E> ... {
• Eliminate unnecessary code ...
duplication
e.g.,
• Ensure compile-time type safety
when operating on different ADTs Vector<Integer> vi = new
Vector<>();
Vector<Double> vd = new
Vector<>();
vi.set(0, 10); // Works
vi.set(0, 10.0); // Fails
vd.set(0, 10.0); // Works
vd.set(0, 10); // Fails
9/23/20 Mobile Application Development
18
• The Java Collections Framework uses generic classes & interfaces extensively
9/23/20 Mobile Application Development
19
• Java’s garbage collector automatically
reclaims & recycles memory that is not
in use by a program
9/23/20 Mobile Application Development
20
• Garbage collection makes writing
many applications much easier
9/23/20 Mobile Application Development
21
• Garbage collection makes writing
many applications much easier
• e.g., it abstracts steps associated
with manually tracking how long Java "xyx"
Heap
each object is used in an app String object eligible
for garbage collection
string1
String reference "abc"
variable
String object
string2 in use
String reference
variable
9/23/20 Mobile Application Development
22
• Java exception handling separates Catches
method1()
“normal” app execution from IOException
“anomalous” app execution
calls
method2() throws JVM searches
up the call
Call Stack
IOException {…}
stack to find
calls exception
handler
method3() throws
IOException {…}
Throws
calls IOException
method4() throw
IOException {…}
Exception handling makes Java apps morerobust, understandable, & evolvable
9/23/20 Mobile Application Development
23
JAVA OOP
9/23/20 Mobile Application Development
24
• Java supports key object-oriented concepts, e.g.
• Data & control abstractions
Abstraction
• Inheritance
• Polymorphism
OOP
Polymorphism Inheritance
9/23/20 Mobile Application Development
25
Abstraction
Abstraction
• Abstraction is an essential part
of all object-oriented
programming languages
• It emphasizes what's important
OOP
& de-emphasizes what’s
unimportant at a particular
level of detail
Polymorphism Inheritance
9/23/20 Mobile Application Development
26
• Java supports many abstractions, e.g.
• Data abstractions
39
9/23/20 Mobile Application Development
27
Data abtraction
• Data Abstraction is the property by virtue of which only the essential details
are displayed to the user.
• Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details.
• The properties and behaviors of an object differentiate it from other objects of
similar type and also help in classifying/grouping the objects.
9/23/20 Mobile Application Development
28
Abstraction is achieved by interfaces and abstract classes
1. An abstract class is a class that is declared with abstract keyword.
2. An abstract method is a method that is declared without an implementation.
3. An abstract class may or may not have all abstract methods. Some of them
can be concrete methods
4. A method defined abstract must always be redefined in the subclass,thus
making overriding compulsory OR either make subclass itself abstract.
5. Any class that contains one or more abstract methods must also be
declared with abstract keyword.
6. There can be no object of an abstract class.That is, an abstract class can
not be directly instantiated with the new operator.
7. An abstract class can have parametrized constructors and default
constructor is always present in an abstract class.
9/23/20 Mobile Application Development
29
Example of Abstract class
9/23/20 Mobile Application Development
30
9/23/20 Mobile Application Development
64
• A Java interface cannot be instantiated, but must be implemented by a class
• The class defines the interfaces methods & any necessary fields
9/23/20 Mobile Application Development
32
Abstract Class vs. Interface
Abstract Class Interface
Abstract keyword is used to create an abstract class and it Interface keyword is used to create an interface but it
can be used with methods. cannot be used with methods.
A class can extend only one abstract class. A class can implement more than one interface.
An abstract class can have both abstract and non-abstract
An interface can have only abstract methods.
methods.
Variables are not final by default. It may contain non-final
Variables are final by default in an interface.
variables.
An abstract class can provide the implementation of an An interface cannot provide the implementation of an
interface. abstract class.
It provides absolute abstraction and cannot have method
It can have methods with implementations.
implementations.
It can have public, private, static and protected access The methods are implicitly public and abstract in Java
modifiers. interface.
It doesn’t support multiple inheritances. It supports multiple inheritances.
It is ideal for code reuse and evolution perspective. It is ideal for Type declaration.
9/23/20 Mobile Application Development
33
Encapsulation vs Data Abstraction
1. Encapsulation is data hiding(information
hiding) while Abstraction is detail
hiding(implementation hiding).
2. While encapsulation groups together data
and methods that act upon the data, data
abstraction deals with exposing the interface
to the user and hiding the details of
implementation.
9/23/20 Mobile Application Development
34
Advantages of Abstraction
1. It reduces the complexity of viewing the things.
2. Avoids code duplication and increases
reusability.
3. Helps to increase security of an application or
program as only important details are provided
to the user.
9/23/20 Mobile Application Development
35
Inheritance Abstraction
• OO languages enhance reuse by
allowing classes to inherit commonly
used state & behavior from other
classes OOP
Polymorphism Inheritance
9/23/20 Mobile Application Development
36
Inheritance
• Super Class: The class whose features are inherited is known as super
class(or a base class or a parent class).
• Sub Class: The class that inherits the other class is known as sub class(or a
derived class, extended class, or child class). The subclass can add its own
fields and methods in addition to the superclass fields and methods.
• Reusability: Inheritance supports the concept of “reusability”, i.e. when we
want to create a new class and there is already a class that includes some of
the code that we want, we can derive our new class from the existing class.
By doing this, we are reusing the fields and methods of the existing class.
9/23/20 Mobile Application Development
37
9/23/20 Mobile Application Development
38
Type of Inheritance
9/23/20 Mobile Application Development
39
• Java Collections Framework demonstrates capabilities & benefits of inheritance
9/23/20 Mobile Application Development
40
• The List hierarchy are collections
that maintain an ordering for
their elements
9/23/20
Overview of Java’s Support Mobile Application Development 28
forthatInheritance
• Subclasses 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()
…
9/23/20 Mobile Application Development 29
• java.lang.Object is the most general
of all classes Object
9/23/20 Mobile Application Development 30
• 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
… …
… …
… …
9/23/20 Mobile Application Development 31
• 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
… …
… …
9/23/20
Overview of Java’s Support Mobile Application Development 32
for Inheritance
• java.lang.Object is the most general
Object
of all classes
• It serves as the root of a
hierarchy of classes available Abstract
Collection …
to Java apps
• Classes towards the bottom of
the inheritance hierarchy are Abstract
List …
more specialized
• e.g., List-related subclasses
override methods inherited Vector ArrayList
from super classes
LinkedList …
9/23/20 Mobile Application Development
46
Polymorphism Abstraction
• Polymorphism enables transparent OOP
customization of methods inherited
from a super class
Polymorphism Inheritance
See en.wikipedia.org/wiki/Pol4ym9 orphism_(computer_science)
9/23/20 Mobile Application Development
47
• The “open/closed principle” can be applied in conjunction with patterns to
enable extensions without modifying existing classes or apps
9/23/20 Mobile Application Development
48
• 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 <<extends>>
<<extends>>
inherit AbstractList methods
Methods in each subclass
emphasize different List properties
9/23/20 Mobile Application Development
49
• Polymorphism in Java occurs when a AbstractMap<K,V>
reference to a super class is used to
refer to an object of a subclass …
• Subclass methods can override entrySet()
put()
super class methods
TreeMap<K,V> HashMap<K,V Concurrent
… … HashMap<K,V
entrySet() entrySet() …
put() put() entrySet()
put()
9/23/20 Mobile Application Development
50
// Create AbstractMap subclass
AbstractMap<K,V>
AbstractMap<Key, Value>
absMap = makeMap(mapType); …
... entrySet()
// Dispatch put() method put()
absMap.put(key, value);
TreeMap<K,V> HashMap<K,V Concurrent
… … HashMap<K,V
entrySet() entrySet() …
put() put() entrySet()
put()
The appropriate method is dispatched at runtime based on the subclass object
9/23/20 Mobile Application Development
51
Home work & prepare for next lession
• Self study
• Reading:
• Core Java Volume I – Fundamentals
• Android Application Development All-in-one for
Dummies
• https://www.geeksforgeeks.org/object-oriented-
programming-oops-concept-in-java/?ref=lbp
• Install Android Studio
• Group choise, Initial GitLab
• Make the first tutorial and submit
Sep 2020 Course Outline Mobile Application Development 52
• [email protected]