Tuesday, 5 November 2013

Oops concepts in java


Java is a pure object oriented programming language. It is based on the concept of object. Java implements all the features of object oriented programming like

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance
  5. Class
  6. Objects
  7. Methods

Object oriented programming provide a great flexibility, reusability, modularity and security to an application.

Data hiding is a very powerful feature. It provides security to various sensitive methods and variables.

Abstraction provides a way to hide the less essential properties so as to reduce complexity and increase efficiency.

Encapsulation is an information hiding and securing mechanism. It helps in restricting the access of data from the outside entities it means only the methods in a class can access its member variables. Encapsulation reduces the system complexity and increases robustness.

Polymorphism is a way to have more than one form of a method. In polymorphism, we use the method overloading and method overriding techniques.

Inheritance is used to reduce the code and reuse the already available code. Here we extend a base class to a derived class. The derived class then inherits the properties of its base class.

Class is a very essential entity in java. Every program in java has a class. Class is like a blueprint of an object. A class contains member variables and methods.

Object is soul of object oriented programming. An object is an instance of its class. Object is used to access the methods and variables in a class.

Methods are the operations that an object can perform. Methods define the behavior of an object.

All of the above object oriented programming concepts are widely used when we write java codes. OOPS concepts are one of the reason for the power and success of java programming language.

Features of java


Java is now one of the most popular programming language. Its success can be traced by its programming features and designs. The “write one run anywhere” is one of the most important feature.
Some of the features of java are as follows,

  1. Object Oriented
  2. Platform Independent
  3. Distributed
  4. Robust
  5. Secure
  6. Multithreaded
  7. High Performance

Object Oriented

Java is a purely object oriented language. Every variable, constant or function is a part of an object. An object or a class is always needed to access its member variables or member functions.

Platform Independent

It is one of the main feature of java. Java is platform independent at source code level i.e. at bytecode level. Java bytecode can be moved from one platform to another platform without any difficulty. Java is a strongly typed language, we have to declare type for every variable. The data types in java are consistent across all platforms. The java interpreter read the bytecode and converts it into the native language of the machine.

Distributed

Using java we can create applications than can run on different platform, GUI and operating system. Java is designed to make distributed computing easy and efficient. Java also provides various libraries for creating network based applications.

Robust

Java is a designed to create highly reliable and robust applications. It checks errors in code during compilation as well as at the time of interpretation. This eliminates various programming errors. Java does not have pointers and pointer arithmetic. In java programmer does not need to bother about memory deallocation as this task is done by garbage collection very effectively.

Secure

Java implements a good security mechanism to protect the system from viruses in code. It assumes that “no code is safe” for execution.

Multithreaded

Multithreading is now a very essential for software applications. Applications have to carry various tasks at a given time. Java provides a very easy mechanism to develop multithreaded applications that are capable to perform various tasks simultaneously.

High performance

Performance plays a very important role in software. It is always believed that execution of bytecode is not as fast as a complied language. But as the processors are advancing this become a myth. Java bytecode executes nearly as good as C or C++

History of Java


Java is now a well know programming language in the world of computers. Its applications range from mobile devices to large complicated machines.

Java was developed by James Gosling & Sun Microsystems (now owned by Oracle Corporation). James Gosling, Mike Sheridan and Patrick Naughton started the java project in June 1991 for interactive television. It was initially called as “Oak”, named after a Oak tree that stood outside Goslings office. Java is developed from taking many of the features of C and C++ language.

Java is very famous for its quotation i.e. “Write Once Run Anywhere (WORA)”. This nature of java programs makes it very popular. With the release of Java 2, it has been divided into 3 main sub categories,

  1. J2SE (Java 2 Standard Edition)
  2. J2EE (Java 2 Enterprise Edition)
  3. J2ME (Java 2 Micro Edition)

J2SE or Java Se
It is a java platform used to deploy portable applications for general use.

J2EE or Java EE
It is a java platform that is used to deploy sever programming. It is different from Java SE as it is used to deploy large java softwares running on an application server.

J2ME or Java ME
It is a java platform that is designed for embedded systems like mobile phones.

There are 5 main features of java language,

  1. It is simple, object oriented and familiar
  2. It is robust and secure
  3. It is architecture neutral and portable
  4. It is executed with high performance
  5. It is interpreted, threaded and dynamic.

These 5 features of java make java as one of the most loved programming language by programmers.

Java scanner


Java Scanner is one of the easiest and fastest way of getting input from user through console. The Scanner class is present in the java.util package. A Scanner object can take input from console or a file. Even if the program has a good GUI to accept user data scanner always prove useful to read data files. A Scanner breaks the input in to token using the delimiter, the default delimiter is white spaces.
A scanner class has 8 different constructors. They are as follows,

  1. Scanner(File source) 
  2. Scanner(File source, String charsetName) 
  3. Scanner(InputStream source)
  4. Scanner(InputStream source, String charsetName) 
  5. Scanner(Readable source) 
  6. Scanner(ReadableByteChannel source) 
  7. Scanner(ReadableByteChannel source, String charsetName) 
  8. Scanner(String source) 

Scanner can be created using any of the above constructor according to need.
Example:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

The Above syntax accepts the input from user through console. Here “nextInt()” method is used to accept the input of integer type. There are methods like “nextDouble()”, “nextFloat()” etc to accept data in their respective format.

Scanner object can also accept data from a file.
Example:
Scanner data1 = new Scanner(new FileStream("filename.txt"));
 while (in.hasNextInt())
{
total +=in.nextInt();
}

The above example accepts all the integer values from the file and add the in the variable “total”. Here the filename is passed to the FileStream constructor to read the file. The “hasNextInt()” method is used to read all the integer values from the file.

The “close()” method is used to close the scanner.
Exmple :
sc.close();

Hence, Java Scanner is one of the convenient way of accepting user data through console or file.

Java string


A String is a sequence of characters, numbers, alphabets and symbols. The String class in java.util package. The String class in java provides number of methods for string manipulation.

Java supports direct string concatenation with “+” symbol, without use of any other method. A String is always read-only and once it is created it cannot be modified.

Creating a string is very simple. Just enclose the string in double quotes and assign it to a literal using assignment operator. String creation through assignment operator does requires new keyword.
Example:
String str=”New String”;
String str1=”Hello Java”;

The String class have 13 constructors overloaded and having different parameters.
  1. String() 
  2. String(byte[] bytes) 
  3. String(byte[] ascii, int hibyte)
  4. String(byte[] bytes, int offset, int length) 
  5. String(byte[] ascii, int hibyte, int offset, int count) 
  6. String(byte[] bytes, int offset, int length, String charsetName) 
  7. String(byte[] bytes, String charsetName) 
  8. String(char[] value)
  9. String(char[] value, int offset, int count)
  10. String(int[] codePoints, int offset, int count) 
  11. String(String original) 
  12. String(StringBuffer buffer)
  13. String(StringBuilder builder)

You can create sting string using any of the above constructor.
Example :
 String str1 = new String();
 String str2=new String(”Hello Java”);

 String str3=new String(str2);

char ch[]={‘H’,’E’,’L’,’L’,’O’};
 String str4=new String(ch);

String is one of the most commonly used class in java.