Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Software for Mobile Devices

Introduction to Java Programming Part (2)


Lecture # 3
Concepts
1. Access Modifiers
2. Inheritance
3. Polymorphism (Overloading/ static, overriding/dynamic binding)
4. Exception Handling (try, catch, finally)
5. Abstract classes/methods and interfaces
6. Static, final method/variables/classes
7. Packages
8. Collections and Data structures.
9. Generics/ Threads (Later in Android Lectures)
10. Garbage Collection (Later in Android Lectures)
Multiple catch blocks

Try catch template


Cases when the finally block doesn’t execute
The circumstances that prevent execution of the code in a finally block are:
1. The death of a Thread
2. Using of the System. exit() method.
3. Due to an exception arising in the finally block.

Will finally block execute??

How did compiler knew to handle


FileNotFound Exception??
Throwing an Exception
Checked exception (compile time) force you to handle them, if you don’t handle them then
the program will not compile.
1. Throws keyword is used ONLY for handling checked exceptions. By using throws
we can declare multiple exceptions in one go.

2. The throw keyword in Java is used to explicitly throw an exception from a method
or any block of code. We can throw either checked or unchecked exception. The
throw keyword is mainly used to throw custom exceptions.
Object Oriented Programming
So, What was abstraction again?

Abstraction: Hiding the internal implementation of the


feature and only showing the functionality to the users. i.e.
what it works (showing), how it works (hiding).

Both abstract class and interface are used for abstraction.

You must know when to use Abstract class and when to use
Interface

Understanding the differences between an abstract class and


interface is key to designing loosely coupled and extensible
applications.

First, identify methods which must be present for that class to work. If you don’t
know those method details/implementations fully or 100% then use interface
otherwise use Abstract class.
Abstract Class VS Interface
1) PARTIAL IMPLEMENTATION VS NO IMPLEMENTATION:

1. Abstract class (0 to 100% Abstract – know partial implementation)


• We know partial implementation detail. So we can implement some of
the functions in this class.
• Those functions which don’t have implementation detail right now, we
can only define them & enforce them to be used by derived classes.
• Also used to provide common implementation to sub-classes. E.g.
ArrayList and LinkedList extends AbstractList (Abstract class) and doesn’t
override subList() function.

2. Interface (100% Abstract - No idea about implementation)


• We don’t know any implementation we just have requirement
specification, so we make interface to enforce these abstract methods
to our child classes for future implementation on later stages.

• It is a contract between concrete classes, every concrete class


implementing interface must need to implement it as well. For Example
Uber and its driver contract.
Abstract Class VS Interface
2) METHODS (CONCRETE vs NO CONCRETE):

1. Abstract class (0 to 100% Abstract)


• Every method present in it, need not to be public and abstract.
• It can also take concrete methods.

2. Interface (100% Pure Abstract Class)


• Every method is public and abstract.
• No concrete methods!!!
Abstract Class VS Interface
3) METHODS (MODIFIERS vs LIMITED MODIFIERS):

1. Abstract class (0 to 100% Abstract)


• We CAN use final, static, synchronized, native, strictfp modifiers for non
abstract methods. BUT We CAN’T use these modifiers for abstract
METHODS,

2. Interface (100% Pure Abstract Class)


• All methods are public by default. (Can’t use private, protected)
• We can’t use following modifiers in Interface methods:
• Final, static, synchronized, native, strictfp (Because they are by
default abstract methods.)
Abstract Class VS Interface
4) VARIABLES (Any modifier vs limited modifier):

1. Abstract class (0 to 100% Abstract)


• Variables present in Abstract class needs not to be public, static, final.

2. Interface (100% Pure Abstract Class)


• Each variable in interface is
• Public
• Static
• Final
• Can’t declare it with:
• Private
• Protected
Abstract Class VS Interface
5) VARIABLES (Initialization):

1. Abstract class (0 to 100% Abstract)


• May not provide initialization at time of declaration.

2. Interface (100% Pure Abstract Class)


• Must provide initialization at time of declaration, because that value will
be final.
Abstract Class VS Interface
6) Object:

1. Abstract class (0 to 100% Abstract)


• We can declare constructor, which will be executed at the time of child
object creation.

2. Interface (100% Pure Abstract Class)


• No constructors.
Abstract Class VS Interface
7) Inheritance:

1. Abstract class (0 to 100% Abstract)


• Can extend only 1 class. (No multiple inheritance)
• Can implement interface as well.
• Those interface functions can be overridden here, or in the concrete
classes.
• If overridden in the Abstract class then no restriction on override
them in the concrete classes.

2. Interface (100% Pure Abstract Class)


• Interface can be implemented, but can’t implement.
• Interface can extend multiple interfaces.
Abstract Class VS Interface
Using Interface to
communicate with
classes

https://github.com/Mughees87/InterfaceCommunicatio
n
Static variables/methods/class
Static variables Static Methods Static class (Nested Class)
A static variable acts like a global A static method can call other Java does have the concept of
variable for all other data members static methods only. nested static class. The outermost
of the class. class can not be made static
whereas the innermost class can
be made static.
A static variable can be accessed A static method can access static A static nested class can not
before any object of the class data only. access the non-static member of
exists. the outer class.
A static variable can be accessed A static method can not be It can only access the static
with the class name in which it is referred to “this” or “super” in any members of the outer class.
defined followed by the dot(.) conditions.
operator.
A static method can be accessed
with the class name in which it is
defined followed by the dot(.)
operation
Final variables/methods/class
Final Static
Final keyword is applicable to class, Static keyword is applicable to
methods and variables. nested static class, variables,
methods and block.
It is compulsory to initialize the final It is not compulsory to initialize the
variable at the time of its static variable at the time of its
declaration. declaration.
The final variable can not be The static variable can be
reinitialized. reinitialized.
Final methods can not be Static methods can only access the
overriden. static members of the class, and
can only be called by other static
methods.
A final class can not be inherited by Static class's object can not be
any class. created, and it only contains static
members only.
Final keyword supports no such Static block is used to initialize the
block. static variables.
Non static inner vs static nested class
• Nested classes are divided into two categories:
• Static (Static Nested Classes)
• Non-static (Inner Classes)
Java Collection Framework
● Common interfaces and implementations
 Collection
● The root interface of all collections

 List:
● Interface representing ordered

collections
● Useful Implementations:

 Vector
 LinkedList
 ArrayList
 Map
● Interface representing an object that

maps keys to values


● Useful Implementations:

 Hashtable
 HashMap
 TreeMap
Java Collection Framework
Collection vs Collection Framework
1. Collection (Parent Interface of all Collection Objects)
2. List (duplicate value allowed, insertion order is saved.)
3. Set (duplication is not allowed and insertion order is not saved.)
4. SortedSet (No duplication allowed but insertion order is saved)
5. NavigationSet (Methods for navigation)
6. Queue (FIFO)
7. Map (key value)
8. SortedMap (key value pair with sorting order)
9. Navigable Map (Methods for navigation)

https://github.com/Mughees87/JavaExamples

You might also like