Java History

1990/91 Initially Developed for Consumer Electronics

requirements - portability, extremely reliable

1994/95 HotJava - first web browser which supported

Java applets - small Java programs that can be
included in HTML file
 

Supported and Marketed by Sun Microsystems

free for non-commercial use

Java Development Kit (JDK) available for Solaris, Windows (95/NT), Linux, Mac.

Current version is JDK 1.5.


What is Java ?

Java is an objected-oriented (OO) programming language.

In OO the focus is on the data in the application and methods (procedure) that manipulate data.

You may need to change your programming style (from purely procedural to OO).

It uses similar language constructs as C.

Java does not use - pointers, records (struct in C), GOTO, etc.

Java supports - arrays, strings, automatic garbage collection, etc.


General Model









Some Features of Java


A Simple Example of Java Application

------------------------------------------------------
/** 
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
     public static void main(String[] args) {
        System.out.println("Hello World!");  //Display the string.
     }
}
------------------------------------------------------

Compiling a Java Source File

% javac HelloWorldApp.java
% java HelloWorldApp






The "Hello World" Applet

----------------------------------
import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld2 extends Applet {
   public void paint(Graphics g) {
     g.drawString("Hello world!", 50, 25);
   }
}
----------------------------------

Compile the Source File

% javac HelloWorld2.java

Create an HTML File that Includes the Applet.

You can do this by creating a file named 'Hello2.html' in the same directory that contains HelloWorld2.class.

This HTML file should contain the following text:

-----------------------------------------
<HEAD>
<TITLE> A Simple Program </TITLE>
</HEAD>
<BODY>

Here is the output of my program:

<APPLET CODE="HelloWorld2.class" WIDTH=150 HEIGHT=25>

</APPLET>

</BODY>
</HTML>
------------------------------------------
Load the HTML File into Java-compatible browser (NetScape).

OR

you can use the Applet Viewer provided in the JDK as below,

% appletviewer Hello2.html
 


Object Oriented Programming (OOP)

In procedural programming languages (like C), programming tends to be action-oriented,

whereas in Java - programming is object-oriented.

In procedural programming,

In OOP, OOP encapsulates data (attributes) and methods (behaviours) into objects, the data and methods of an object are intimately tied together.

Objects have the property of information hiding .


Inheritance in OOP

Inheritance is a form of software reusability in which new classes are created from the existing classes by absorbing their attributes and behaviours.

Instead of defining completely (separate) new class, the programmer can designate that the new class is to inherit attributes and behaviours of the existing class (called superclass). The new class is referred to as subclass.

Programmer can add more attributes and behaviours to the subclass, hence, normally subclasses have more features than their superclasses.

Inheritance relationships form tree-like hierarchical structures.

For example,
 


 


OO Programming - General Hints

Designing Class Hierarchy

"Is-a" relationship

"Has-a" relationship Important : Getting "Is-a" versus "Has-a" relationships correct is both subtle and potentially critical. You should consider all possible future usages of the classes before finalising the hierarchy. It is possible that obvious solutions may not work for some applications.
Designing a Class

Think carefully about the functionality (methods) a class should offer.

Always try to keep data private (local).

Consider different ways an object may be created.

Creating an object may require different actions such as initializations.

Always initialize data.

If the object is no longer in use, free up all the associated resources.


Introduction to Classes and Objects

A class is a collection of data and methods (procedures) that operate on that data.

For example,

a circle can be described by the x, y position of its centre and by its radius.

We can define some useful methods for circles,

By defining the Circle class (as below), we can create a new data type.
// The class of circles (partially defined) 
public class Circle {
}









Objects are Instances of a class

In Java, objects are created by instantiating a class.

For example,

Circle c ;
c = new Circle();
Circle d = new Circle();


Accessing Object Data

We can access data fields of an object.

For example,

Circle c = new Circle();
//initialize our circle to have centre (2, 5) and radius 1.0. 
c.x = 2.0;
c.y = 5.0;
c.r = 1.0;
// let's create another circle
Circle d = new Circle();
//initialize this circle to centre (10, 7) and radius 4.0. 
d.x = 10.0;
d.y = 7.0;
d.r = 1.0;









Using Object Methods

To access the methods of an object, we can use the same syntax as accessing the data of an object :
Circle c = new Circle();
double a;
c.r = 2.5;
a = c.area();
//Note that it is not a = area(c)









Subclasses and Inheritance

We want to implement GraphicalCircle.

This can be achieved in at least 3 different ways ….

First approach ….

Second approach …… Third approach ……

Superclasses, Objects, and the Class Hierarchy

Every class has a superclass.

If we don't define the superclass, by default, the superclass is the class Object.

Object Class :