JAVA Tutorial II

Click to rate this tutorial!
[Total: 0 Average: 0]

Java is an object-oriented programming language developed by Sun Microsystems and released in 1995. Modelled after C++, the Java language was designed to be small, simple, and portable across platforms and operating systems, at both the source and binary level. One of the resons for Java’s popularity is its ability to bring executable content to the web.Java has only the necessary functionality needed to implement its feature set. It has omitted the features of C and C++ which have been considered to be “unsafe” pointer “forging” operator overloading static objects Memory is managed automatically, relieving the programmer from being responsible for freeing unused space There is no Java preprocessor – the program that you see is the same program that the Java compiler sees.

 
Let The Games Begin…

CONCEPTS OF OOPS
As you know all computer programs consist of two elements: code and data.
Furthermore, a program can be conceptually organized around its code or around its data. That is some programs are written around “‘what is happening” and other are written around “who is being affected”. These are two paradigms that govern how a program is constructed. The first way is called process oriented model. This approach characterizes a program as a series of linear steps. The process oriented model can be thought of as code acting on data. Procedural languages such as C employ this model to considerable success. To manage increasing complexity the second approach called object oriented programming was conceived. Object oriented programming organizes a program around its data( that is its object) and a set of well defined interfaces to that data. An object oriented program can be characterized as data controlling access to code.

CLASSES
The importance of object-oriented programming is that it enables programmers to organize their programs in ways that resemble the organization of objects in the real world, which makes programming easier and more logical. Using object- oriented programming, the overall programs are made up of many different components (called “objects”), each of which has a specific role in a program and can talk to the other components in predefined ways.Classes are called as a template or they can be called as a prescription of an object.
Without class no object can exist and every object has a class.

OBJECTS
Every thing in this world is called an object. An object is created by the method and data in the class.
METHODS
Methods are nothing but functions. In procedural languages these are called functions. In OOP they are called method. It is a function that can be used by the object or an object can be created by the method.
PIE FEATURES OF OOPS
There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.
ENCAPSULATION
Encapsulation is a mechanism that binds together code and data it manipulates, and keeps both safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
INHERITANCE
Inheritance is the process by which one object acquires the properties of another object.
This is important because it supports the concept of hierarchical classification. Most knowledge is made by hierarchical classification.
POLYMORPHISM
It is a feature that allows one interface to be used for general class of actions. The specific action is determined by the exact nature of the situation. More generally the concept of polymorphism is often expressed by the phrase “one interface, multiple methods”.
WHAT IS JAVA?
Java is an object oriented programming language. Java is an enhanced form of C++. From C java drives its syntax. Many of java’s object oriented features are influenced by C++. Java is platform independent. It can run on any platform. Java has a compiler and an interpreter.
Its compiler generates intermediate code which is the further interpreted by its interpreter.
PROGRAMMING STRUCTURE OF JAVA
Now lets look at the programming structure of java. Every program should be started from the reserved word class. Let us see a sample program.
Class Example {
Public static void main(String arg[]) {
System.out.println(“this is first program in java”);
}
}

every program of java must be saved in the BIN folder of the JDK. The file’s extension should be somefile.java. the file name should be same as the name of class. In this case it would be Example.java.
CONTROL STATEMENTS IN JAVA
Java has two control statements which are if and else if. They are used as follows.

If (condition) statements
Else
Statements
Parenthesis can also be used if there are more than one statements in if or else closure.

LOOPS IN JAVA
There are three types of loops in java, for, while and do while. The syntax of loops is as follows.
For(declaration;condition; increment/decrement)

{
statemenst

}
while(condition)

{
statements

}
do
{
statements
}while(condition)

OPERATORS IN JAVA
There are different kinds of operators in java which are as follows.
< less than
> greater than
= = equal to
= assignment operator
? ternary operator
| | Boolean OR
&& Boolean AND
++ increment operator
— decrement operator
% Modulus
+= addition assignment
-= subtraction assignment
*= multiplication assignment
%= modulus assignment
>> shift right
<< shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
ARRAYS IN JAVA
An array is a group of like-typed variables that are referred to by a common name. Array of any type can be created and may have one or more dimensions.
ONE DIMENSIONAl ARRAY
A one dimension array is essentially a list of like typed variables. To create an array you first must create an array variable of desired type. The general form of the one dimension array is like as follow.
Type var-name[];

Here type declares the base type of the array. The base type determines the data type of each element that comprises an array. For example the following declares an array named month_days with type “:array of int”:

Int month_days[];

MULTIDIMENSIONAL ARRAYS
In java multidimensional arrays are actually arrays of arrays. These as you might expect look and act like regular multidimensional arrays. However as you will see there are a couple of differences. To declare a multidimensional array variable, specify each additional index using another set of square brackets. For example the following declares a two dimensional array variable called twoD.

Int twoD[][]= new int[4][5]

CLASSES AND OBJECTS IN JAVA
When you define a class you declare its exact form and nature. You do this by specifying the data that it contains and the code that operates on that data. While very simple classes may contain only code data, most real world classes contain both.
A class is declared by the use of the class keyword. General form of class declaration is as follows.
Class classname
{

type instance variable1
type instance variable2

type methodname(parameter list)
{
// body of method
}

to create an object of a certain class we use a new word new. Consider an example.
Class myclass
{

}
classname objectname=new classmethod();

CLOSER LOOK AT NEW
As explained the new operator allocates memory for an object. It has genral form as:
Class-var=new classname();
Here class-var is a variable of the class type being created. The class name followed by parentheses specifies the constructor for the class. A constructor defines what occurs when an object of a class is created. Constructors are important part of all classes and have many significant attributes.

CONSTRUCTORS OF JAVA
A constructor is a method which is invoked when an object of a class is created. It has no return type in java. Its name must be same as the name of class. It can accept parameter . here we look at how they function in java.

Class student
{
int age;
string name;

student() //constructor
{
age=0;
name=”junaid”;
}

in java the overloading of constructors is possible. An overloading means using same name of constructor with different parameters. An example is given.

Class student
{
int age;
string name;

student() //constructor
{
age=0;
name=”junaid”;
}

student(int n, string s) // constructor overloading
{
age=n;
name=s;
}

DESTRUCTORS IN JAVA
In C++ dynamically allocated objects must be manually released by use of a delete operator. Java uses different approach. the technique is called

GARBAGE COLLECTION.
Java uses finalize() method for garbage collection. To add a finalize to a class you simply define the finalize() method. The java run time calls that method whenever it is about to recycle an object of that class. Inside the finalize() method you will specify those actions that must be performed before an object is destroyed. Tha garbage collector runs periodically checking for objects that are no longer referenced by any running state or indirectly though other referenced objects.
void finalize()
{
finalization code
}

METHODS IN JAVA
Methods in java can have return type and can accept the parameters. They can be called without creation of objects. These methods are called static methods. Following is an example of methods.

Class junaid
{
int empno;
void jd()
{
empno=0;
}
void display()
{
System.out.print(“employee number”+empno)
}
}
class demo
{
public static void main(String arg[])

{
emp A=new emp();
A.jd();
A.diisplay();
}
}

methods in java can be overloaded and override .
INHERITANCE IN JAVA
Inheritance means the parent class can have some child classes and the child classes can have or can inherit all the properties of parent class. The parent class can also be called as super class and the child class can be called as subclass.
A keyword extends is used in order to make a child class of parent class. Following is an example of this .

Class a
{
//variables
}
class b extends a
{
// variables
}

all the variables of a super class cannot be accessed by the base class except of those which are public. Private members of a super class cannot be accessed by the child class.
Similarly constructors of a super class can also be used by the child class. To call a method of a super class through a base class, we use a keyword super. This can be used to call a method of super class. Following is an example.

Class A
{
// class variables
void method()
{
System.out.print(“hi”);
}
}
class B extends A
{
// class variable
void method2
{
super.method();
System.out.println(“its me”);
}
}
class demo
{
public static void main(String arg[])
{
B a=new B();
B.method2();
}
}

similarly multilevel inheritance is also possible in java. For example a parent class A can have two child classes B and C.
ABSTRACT CLASSES
Abstract classes are those which can be used for creation of objects. However their methods and constructors can be used by the child or extended class. The need for abstract classes is that you can generalize the super class from which child classes can share its methods. The subclass of an abstract class which can create an object is called as “concrete class”. Following is an example
Abstract class A
{
abstract void method1();
void method2()
{
System.out.println(“this is concrete method”);
}
}
class B extends A
{
void method1()
{
System.out.println(“B is implementation of method1”);
}
}
class demo
{
public static void main(String arg[])
{
B b=new B();
b.method1();
b.method2();
}
}

Click to rate this tutorial!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published.