Java Tutorial for Bachelor in Computer Science and Information Technology, Thesis of Information Technology

Advanced java tutorial for Bachelor in Computer Science and Information Technology

Typology: Thesis

2016/2017

Uploaded on 07/20/2017

subash-giri
subash-giri 🇳🇵

1 document

1 / 231

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
unit 1.pdf
Unit2and3_user interface with swing.pdf
Unit4Database Connectivity.pdf
Unit5Network Programming.pdf
Unit6Java Beans.pdf
Unit7Servlets and JSP.pdf
Unit8RMI and CORBA.pdf
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Java Tutorial for Bachelor in Computer Science and Information Technology and more Thesis Information Technology in PDF only on Docsity!

unit 1.pdf

Unit2and3_user interface with swing.pdf

Unit4Database Connectivity.pdf

Unit5Network Programming.pdf

Unit6Java Beans.pdf

Unit7Servlets and JSP.pdf

Unit8RMI and CORBA.pdf

Class The class is at the core of Java. It is the logical construct upon which the entire Java language is built because it defines the shape and nature of an object. As such,the class forms the basis for object-oriented programming in Java. Any concept you wish to implement in a Java program must be encapsulated within a class.

Perhaps the most important thing to understand about a class is that it defines a new data type. Once defined, this new type can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class. Because an object is an instance of a class, you will often see the two words object and instance used interchangeably.

The general form of class

class classname { type instance-variable1; type instance-variable2; // ... type instance-variableN; type methodname1(parameter-list) { // body of method } type methodname2(parameter-list) { // body of method } // ... type methodnameN(parameter-list) { // body of methodN } }

The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class.

Example of Class class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; } // sets dimensions of box

initialization is performed through the use of a constructor. A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.

/* Here, Box uses a constructor to initialize the dimensions of a box.*/

class Box { double width; double height; double depth;

// This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; }

// compute and return volume double volume() { return width * height * depth; } } class BoxDemo6 { public static void main(String args[]) {

// declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(); double vol;

// get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol);

// get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } When this program is run, it generates the following results: Constructing Box Constructing Box

Volume is 1000. Volume is 1000.

Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded , and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism. When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the type and/or number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.

// Demonstrate method overloading. class OverloadDemo { void test() { System.out.println("No parameters"); }

// Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a); }

// Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); }

// overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result;

// call all versions of test()

return width * height * depth; } } class OverloadCons { public static void main(String args[]) {

// create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); double vol;

// get volume of first box vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol);

// get volume of second box vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol);

// get volume of cube vol = mycube.volume(); System.out.println("Volume of mycube is " + vol); } }

The output produced by this program is shown here: Volume of mybox1 is 3000. Volume of mybox2 is -1. Volume of mycube is 343.

Static Variables and Methods There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static.

The most common example of a static member is main( ). main( ) is declared as static because it must be called before any objects exist. Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

Methods declared as static have several restrictions: ■ They can only call other static methods. ■ They must only access static data. ■ They cannot refer to this or super in any way. (The keyword super relates to inheritance.) If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded.

// Demonstrate static variables, methods, and blocks. class UseStatic { static int a = 3; static int b;

static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); }

static { System.out.println("Static block initialized."); b = a * 4; }

public static void main(String args[]) { meth(42); } }

Here is the output of the program: Static block initialized. x = 42 a = 3 b = 12

As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.

Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. classname.method( )

This program displays the following output: length of a1 is 10 length of a2 is 8 length of a3 is 4

Inheritance Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a superclass. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements.

To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword.

// A simple example of inheritance.

// Create a superclass. class A { int i, j; void showij( ) { System.out.println("i and j: " + i + " " + j); } }

// Create a subclass by extending class A. class B extends A { int k; void showk( ) { System.out.println("k: " + k); } void sum( ) { System.out.println("i+j+k: " + (i+j+k)); } }

class SimpleInheritance { public static void main(String args[ ]) { A superOb = new A( );

B subOb = new B();

// The superclass may be used by itself. superOb.i = 10; superOb.j = 20; System.out.println("Contents of superOb: "); superOb.showij(); System.out.println();

// The subclass has access to all public members of its superclass. subOb.i = 7; subOb.j = 8; subOb.k = 9; System.out.println("Contents of subOb: "); subOb.showij( ); subOb.showk( ); System.out.println( ); System.out.println("Sum of i, j and k in subOb:"); subOb.sum( ); } }

The output from this program is shown here: Contents of superOb: i and j: 10 20 Contents of subOb: i and j: 7 8 k: 9 Sum of i, j and k in subOb: i+j+k: 24

As you can see, the subclass B includes all of the members of its superclass, A. This is why subOb can access i and j and call showij( ). Also, inside sum( ), i and j can be referred to directly, as if they were part of B.

Note: Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.

Super Keyword super has two general forms.  The first calls the superclass’ constructor.  The second is used to access a member of the superclass that has been hidden by a member of a subclass.

Using super to Call Superclass Constructors

double weight; // weight of box

// construct clone of an object BoxWeight(BoxWeight ob) { // pass object to constructor super(ob); weight = ob.weight; }

// constructor when all parameters are specified BoxWeight(double w, double h, double d, double m) { super(w, h, d); // call superclass constructor weight = m; }

// default constructor BoxWeight() { super(); weight = -1; }

// constructor used when cube is created BoxWeight(double len, double m) { super(len); weight = m; } } class DemoSuper { public static void main(String args[]) { BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3); BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076); BoxWeight mybox3 = new BoxWeight(); // default BoxWeight mycube = new BoxWeight(3, 2); BoxWeight myclone = new BoxWeight(mybox1); double vol; vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); System.out.println("Weight of mybox1 is " + mybox1.weight); System.out.println(); vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); System.out.println("Weight of mybox2 is " + mybox2.weight); System.out.println(); vol = mybox3.volume(); System.out.println("Volume of mybox3 is " + vol); System.out.println("Weight of mybox3 is " + mybox3.weight);

System.out.println(); vol = myclone.volume(); System.out.println("Volume of myclone is " + vol); System.out.println("Weight of myclone is " + myclone.weight); System.out.println(); vol = mycube.volume(); System.out.println("Volume of mycube is " + vol); System.out.println("Weight of mycube is " + mycube.weight); System.out.println(); } } This program generates the following output: Volume of mybox1 is 3000. Weight of mybox1 is 34. Volume of mybox2 is 24. Weight of mybox2 is 0. Volume of mybox3 is -1. Weight of mybox3 is -1. Volume of myclone is 3000. Weight of myclone is 34. Volume of mycube is 27. Weight of mycube is 2.

A Second Use for super This second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass. This usage has the following general form: super.member Here, member can be either a method or an instance variable.

Consider this simple

// Using super to overcome name hiding. class A { int i; int j; }

// Create a subclass by extending class A. class B extends A { int i; // this i hides the i in A

B(int a, int b,int c) { super.i = a; // i in A i = b; // i in B

package java.awt.image; needs to be stored in java\awt\image on the Windows file system.

Access Modifiers The three access specifiers, private, public, and protected , provide a variety of ways to produce the many levels of access required by these categories. Table below sums up the interactions

An Access Example

//This is file Protection.java: package p1; public class Protection { int n = 1; private int n_pri = 2; protected int n_pro = 3; public int n_pub = 4;

public Protection() { System.out.println("base constructor"); System.out.println("n = " + n); System.out.println("n_pri = " + n_pri); System.out.println("n_pro = " + n_pro); System.out.println("n_pub = " + n_pub); } }

//This is file Derived.java:

package p1; class Derived extends Protection { Derived() { System.out.println("derived constructor");

System.out.println("n = " + n); // class only // System.out.println("n_pri = " + n_pri); System.out.println("n_pro = " + n_pro); System.out.println("n_pub = " + n_pub); } }

//This is file SamePackage.java: package p1; class SamePackage { SamePackage() { Protection p = new Protection(); System.out.println("same package constructor"); System.out.println("n = " + p.n); // class only // System.out.println("n_pri = " + p.n_pri); System.out.println("n_pro = " + p.n_pro); System.out.println("n_pub = " + p.n_pub); } }

//This is file Protection2.java: package p2; class Protection2 extends p1.Protection { Protection2() { System.out.println("derived other package constructor"); // class or package only // System.out.println("n = " + n); // class only // System.out.println("n_pri = " + n_pri); System.out.println("n_pro = " + n_pro); System.out.println("n_pub = " + n_pub); } }

//This is file OtherPackage.java: package p2; class OtherPackage { OtherPackage() { p1.Protection p = new p1.Protection(); System.out.println("other package constructor"); // class or package only // System.out.println("n = " + p.n); // class only

either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package. This code fragment shows both forms in use:

import java.util.Date; import java.io.;*

Any place you use a class name, you can use its fully qualified name, which includes its full package hierarchy. For example, this fragment uses an import statement: import java.util.; class MyDate extends Date { }*

The same example without the import statement looks like this: class MyDate extends java.util.Date { }

NOTE: When a package is imported, only those items within the package declared as public will be available to non-subclasses in the importing code.

For example, if you want the Balance class of the package MyPack to be available as a stand-alone class for general use outside of MyPack, then you will need to declare it as public and put it into its own file, as shown here:

package MyPack; /* Now, the Balance class, its constructor, and its show() method are public. This means that they can be used by non-subclass code outside their package. */ public class Balance { String name; double bal;

public Balance(String n, double b) { name = n; bal = b; }

public void show() { if(bal<0) System.out.print("--> "); System.out.println(name + ": $" + bal); }

As you can see, the Balance class is now public. Also, its constructor and its show() method are public, too. This means that they can be accessed by any type of code outside the MyPack package. For example, here TestBalance imports MyPack and is then able to make use of the Balance class:

import MyPack.*;

class TestBalance { public static void main(String args[]) { /* Because Balance is public, you may use Balance class and call its constructor. */ Balance test = new Balance("J. J. Jaspers", 99.88); test.show(); // you may also call show() } }

Experiment - Remove the public specifier from the Balance class and then try compiling TestBalance. As explained, errors will result.

Interfaces Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. In practice, this means that you can define interfaces which don’t make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces.

Using interface, you can specify what a class must do, but not how it does it.

To implement an interface, a class must create the complete set of methods defined by the interface. However, each class is free to determine the details of its own implementation. By providing the interface keyword, Java allows you to fully utilize the “one interface, multiple methods” aspect of polymorphism. Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible.

Defining an Interface An interface is defined much like a class. This is the general form of an interface:

access interface name {