Search This Blog

Tuesday, August 25, 2009

Inheritance in Java

Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.




Java  langauge supports single& multiple inheritance,but it doesn't support multilevel inheritance.



It allows you to extend a class (called the base
class or superclass) with another class (called the derived class or subclass).
 

in java ,'extends' keyword is used to achieve inheritance,for example:




public class A{
}

public class M extends Al{
}

public class R extends A{
}

public class D extends M{
}


 

Now based on the above example,we get:




A is the superclass of M class.

A is the superclass of R class.

M and Reptile are sub classes of A class.

D is the subclass of both M and A  classes.





Note: With use of the extends keyword the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass. 




The following kinds of inheritance are there in java:
 
  Simple Inheritance


  Multilevel Inheritance


 

Pictorial Representation of Simple and Multilevel Inheritance




             Single Inheritance                                                               Multiple Inheritance


                                     

 

Wednesday, August 12, 2009

Command line Argument

Before learning how to pass command line arguments,we first need to know about command line arguments.

While running the program any things written after the name of the class are the command line arguments.We can pass any number to the java program through command line. Arguments are delimited by the space in command line.

for example : java [class name] arg1 arg2 arg3 arg4..................
....................................


Parsing Numeric Command-Line Arguments

If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "100", to a numeric value. Here is a code snippet that converts a command-line argument to an int:
class cla
{
public static void main(String a[])
{

int i;
if(a.length>0)
{System.out.println("commandline arguments:" );
for(int j=0;j<=a.length;j++) { i=Integer.parseInt(a[j]); System.out.println(i);} } }}



compiling and running this program code:







Number classes — Integer, Float, Double, and so on — have parseXXX String methods that convert a representing a number to an object of their type.

Saturday, August 8, 2009



First Program in Java

Writing code


You will need to write your Java programs using a text editoror we can use Notepad for this.

When you type the examples that follow you must make sure that you use capital and small letters in the right places because Java is case sensitive.



We must create the main method which is the section that a program starts.



class abc 
{
public static void main(String p[] )
{
System.out.print("my first java program");
}
}



Make sure that you use a capital S in System because it is the name of a class. println is a method that prints the words that you put between the brackets after it on the screen. When you work with letters like in Hello World you must always put them between quotes. The semi-colon is used to show that it is the end of your line of code.


Compiling the Code




What we have just finished typing is called the source code.we have to save this source code file with the extension .java such as ' abc.java'.

Note: The file name must always be the same as the class name.

now we have to compile this code,for this open cmd(command prompt) and enterthis

javac abc.java








If you did everything correct then you will see no errors message and your program will be compiled.



Running the program




Once your program has been compiled you will get a file called abc.class. This is not like normal programs that you just type the name to run but it is actually a file containing the Java bytecode that is run by the Java interpreter. To run your program with the Java interpeter use the following command:


java abc



Do not add .class on to the end of Hello. You will now see the following output on the screen:

my first java program

Congratulations! You have just made your first Java program.
Compile and Run a Java Program

This tutorial will take you step by step through the process of writing a java program, compiling and running it.
Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do.

but before this we have to make some changes,there are many ways to do,but here are the 2 simplest ways:

1) create shortcut on your desktop of CMD type,give it some name say 'java'.

2) open jdk folder in the drive you installed jdk,then open the bin folder,copy the path of bin folder.
3) open the properties of the shortcut you created and paste this path in startin field of the properties,then click on 'ok' button,now the job is done.

now taking the 2nd way:

1) open jdk folder in the drive you installed jdk,then open the bin folder,copy the path of bin folder.
2) Open the properties of 'My computer' ,click the Advanced tab on top.

2) then open the environment variables,in it click on new button.

3) enter the variablename as Path and variablepath as path of bin that you have copied.

4) then click the 'ok ' button.

after you have made the changes,you are now ready to do your first java program.
Java Platform

The Java platform consists of the Java application programming interfaces (APIs) and the Java virtual machine (JVM).

Java APIs are libraries of compiled code that you can use in your programs. They let you add ready-made and customizable functionality to save you programming time.

The simple program in this lesson uses a Java API to print a line of text to the console. The console printing capability is provided in the API ready for you to use; you supply the text to be printed.

Java programs are run (or interpreted) by another program called the Java VM(JVM).Rather than running directly on the native operating system, the program is interpreted by the Java VM for the native operating system. This means that any computer system with the Java VM installed can run Java programs regardless of the computer system on which the applications were originally developed.

For example, a Java program developed on a Personal Computer (PC) with the Windows NT,Xp,vista operating system should run equally well without modification on a Sun Ultra workstation with the Solaris operating system,linux, and vice versa.

Installing the Java Development Kit for Windows:

To get started with Java We will need Java Development Kit(JDK 5.0 or 1.5),which You can download from this URL :

http://java.sun.com/javase/downloads/index_jdk5.jsp

if you find any difficulty in installing JDK then,take help of this video:

Please See this Video For Help

Friday, August 7, 2009








Before We Start learning Java ,we should know these important things:

what is Java?


Advantages of java over c++ ?
  1. Java's biggest advantage is portability of code and its power lies in its APIs.It offers support to various types of applications and is a pure Object oriented language,while C++ is not.It is not Platform independent,but performance wise it is better than Java.

read more>