Professional Documents
Culture Documents
JAVA Documentation and Android Application Development Basics
JAVA Documentation and Android Application Development Basics
I. OOP Rewind
One most easily relates C++ to Object Oriented Programming. However, unlike C++, where
OOP is optional, Java is purely OOP language. Hence let us quickly revise some of those
essential OOP concepts flavoured with Java-
• Inheritance allows hierarchical classification. Simply put, a class can inherit data and
methods of another class.
• A class that is inherited is a superclass. The class that does the inheriting is a
subclass.
• Thus, a subclass inherits all the instance variables and methods defined by the
superclass and add its own, unique ones.
• In Java, a class inherits another class by keyword extends written as-
• Interestingly, reference variable of a superclass can be assigned a reference to any
subclass derived from that superclass.
• Overriding: When the superclass and subclass both define a method with the same
name, method of subclass is referred when invoked. To override this method and invoke
the method of superclass super.method(); is used. The constructor of the superclass
can also be invoked similarly by writing super(); inside the subclass with/without
parameters.
• Abstraction: is what you use to create a superclass that only defines a
generalized form that will be shared by all of its subclasses, leaving it to each
subclass to fill in the details. Such methods can be declared as abstract.
However, if any method in a class is declared as abstract, the class should be
declared as abstract. Consider-
abstract class A {
abstract void callme(); // concrete methods are still allowed in
// abstract classes
void callmetoo() {
System.out.println("This is a concrete method."); }}
Access Specifiers
• Similar to C++, Java’s access specifiers are public, private, and protected (applies
only when inheritance is involved)
• These specifiers can be applied to different classes in a package as well as individual
data and methods inside a class.
@override
void onClick(){ // This method belongs to the interface inter1
• Packages are containers for classes that are used to keep the class namespace
compartmentalized. Packages are stored in a hierarchical manner.
• The simplest form of defining a package-
package pkg;
• Hierarchical form- package pkg1[.pkg2[.pkg3]];
//for e.g., java.awt.image;
II. Some more Java
Exception Handling
• Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java
language or the constraints of the Java execution environment.
• Java exception handling is managed via five keywords: try, catch, throw, throws, and
finally.
• Program statements that you want to monitor for exceptions are contained within a try
block. If an exception occurs within the try block, it is thrown. Your code can catch this
exception (using catch) and handle it in some rational manner.
• To manually throw an exception, use the keyword throw. Any exception that is thrown
out of a method must be specified as such by a throws clause.
• Any code that absolutely must be executed before a method returns is put in a finally
block.
This is the general form of an exception-handling block:
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed before try block ends
}
What is Android?
Android is a software stack for mobile devices that includes an operating system, middleware and key
applications. The Android SDK provides the tools and APIs necessary to begin developing applications
on the Android platform using the Java programming language.
Features
Application Fundamentals
• Android applications are written in the Java programming language.
• Android application user interface can be written in Java or XML
• If written in separate XML files code is readable because application logic and
UI gets separated
•
XML Basics
What is XML?
View objects are the basic units of user interface expression on the Android platform. The View
class serves as the base for subclasses called "widgets," which offer fully implemented UI
objects, like text fields and buttons
Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the
same way you create web pages in HTML — with a series of nested elements.
Each layout file must contain exactly one root element, which must be a View or ViewGroup object. Once you've
defined the root element, you can add additional layout objects or widgets as child elements to gradually build a
View hierarchy that defines your layout. For example, here's an XML layout that uses a vertical LinearLayout to
hold a TextView and a Button:
1. Activity
Activities
An activity presents a visual user interface for one focused endeavor the user can undertake. For
example, an activity might present a list of menu items users can choose from or it might display
photographs along with their captions. A text messaging application might have one activity that
shows a list of contacts to send messages to, a second activity to write the message to the chosen
contact, and other activities to review old messages or change settings. Though they work
together to form a cohesive user interface, each activity is independent of the others. Each one is
implemented as a subclass of the Activity base class.
2. Service
Services
A service doesn't have a visual user interface, but rather runs in the background for an indefinite
period of time. For example, a service might play background music as the user attends to other
matters, or it might fetch data over the network or calculate something and provide the result to
activities that need it. Each service extends the Service base class.
3. BroadcastReceiver
Broadcast receivers
A broadcast receiver is a component that does nothing but receive and react to broadcast
announcements. Many broadcasts originate in system code — for example, announcements that
the timezone has changed, that the battery is low, that a picture has been taken, or that the user
changed a language preference. Applications can also initiate broadcasts — for example, to let
other applications know that some data has been downloaded to the device and is available for
them to use.
An application can have any number of broadcast receivers to respond to any announcements it
considers important. All receivers extend the BroadcastReceiver base class.
4. ContentProvider
Content providers
A content provider makes a specific set of the application's data available to other applications.
The data can be stored in the file system, in an SQLite database, or in any other manner that
makes sense. The content provider extends the ContentProvider base class to implement a
standard set of methods that enable other applications to retrieve and store data of the type it
controls