You are on page 1of 10

JAVA documentation and Android

Application Development Basics

Hello Java..!
Java is an object oriented programming language developed by Sun Microsystems. It looks lot
like C and C++ but has various extra-developed features that make it safer, easier and more
useful than other object oriented languages. The most important feature of Java is that it can
run on different types of computers without any modification.

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-

Classes, Objects and Constructors

• A class is a structure that defines the data and the methods to work on that data.
• Objects of the class are declared in 2 steps-
1) declare a reference variable of the class type
2) acquire an actual, physical copy of the object and assign it to that variable.
• A constructor initializes an object immediately upon creation by automatic invocation. It
has the same name as the class in which it resides and is syntactically similar to a
For example, consider following program “”-
class Student { //declaration of class
String name[];
int rollNo;
String branch[];

Student() { //simple constructor which will assign given values to

name = “”; // every new object created without parameters
rollNo = 0; }

Student(int rno, String b) {//parameterised constructor, which will be

rollNo = rno; //invoked when parameterised obj is created
branch = b; }

void setRollNo(int rno) { //this is a method

rollNo = rno; }

public static void main(String args[]) { //main function

Student stud,stud1; //create 2 reference variables
stud = new Student(); //initialise using simple constructor
stud1=new Student(10,comp); //invoke parameterised
stud.setRollNo(20); //method invoked
Inheritance, Abstraction and Overriding

• 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
• 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.

//class B defined somewhere earlier

class A extends class B { //class A inherits class B
.... //define data and methods of class A
//in main() method
classA objA = new objA();
classB objB = new objB();

objA=objB;//reference of subclass assigned to reference of 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
• 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.

Interfaces and Packages

• As you may have heard, Java supports all kinds of inheritance (viz. single, hybrid,
hierarchical and multilevel inheritance) except multiple inheritance i.e. a subclass
cannot inherit from more than one superclass.
• An alternative for it- interfaces. Using interface, you can specify what a class must do,
but not how it does it. So in a way it provides a template that has to be developed by the
class implementing the interface.
• Once it is defined, any number of classes can implement an interface. Also, one class
can implement any number of interfaces.
• To implement an interface, a class must implement the complete set of methods defined
by the interface.

class ABC implements inter1{

void onClick(){ // This method belongs to the interface inter1

// Put in your own implementation of this method here


A class implements multiple interfaces as follows-

class A implements inter1 implements inter2...

• 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
• 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
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

III. Have you watched Eclipse? (Not the movie!)

• Eclipse is an extensible, open source IDE (integrated development environment).

Perhaps the most interesting feature of Eclipse is that it is completely platform- neutral
and language-neutral.
• In addition to the eclectic mix of languages supported by the Eclipse Consortium (Java,
C/C++, Cobol), there are also projects underway to add support for languages as
diverse as Python, Eiffel, PHP, Ruby, and C# to Eclipse.
• We are going to use this IDE to get started with Java programming. Here is what a
snapshot of it looks like-
Editor Window
Project (where u write
explorer ur code)

Compiler and debug window (u can view

errors and monitor watches here)

A snapshot of Eclipse v3.1


Android Application Development

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.


• Application framework enabling reuse and replacement of components

• Dalvik virtual machine optimized for mobile devices
• Integrated browser based on the open source WebKit engine
• Optimized graphics powered by a custom 2D graphics library; 3D graphics based
on the OpenGL ES 1.0 specification (hardware acceleration optional)
• SQLite for structured data storage
• Media support for common audio, video, and still image formats (MPEG4, H.264,
• GSM Telephony (hardware dependent)
• Bluetooth, EDGE, 3G, and WiFi (hardware dependent)
• Camera, GPS, compass, and accelerometer (hardware dependent)
• Rich development environment including a device emulator, tools for
debugging, memory and performance profiling, and a plugin for the Eclipse IDE

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


Your layout is the architecture for the user interface in an Activity. It defines the layout structure and holds
all the elements that appear to the user. You can declare your layout in two ways:
Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the
View classes and subclasses, such as those for widgets and layouts.
Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and
manipulate their properties) programmatically.

XML Basics
What is XML?

• XML stands for EXtensible Markup Language

• XML is a markup language much like HTML
• XML was designed to carry data, not to display data
• XML tags are not predefined. You must define your own tags
• XML is designed to be self-descriptive

More on User Interface

In an Android application, the user interface is built using View and ViewGroup objects. There
are many types of views and view groups, each of which is a descendant of the View class.

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

Write the XML

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:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:text="Hello, I am a Button" />

Key classes For Application Development

1. Activity

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

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

5. Intent : More on it in the workshop 