Show Mobile Navigation

Webring

Powered by WebRing.

Features of object oriented programming language

Sachin R Kukale - 02:17
In Object Oriented Programming program or an application is divided and organised using the objects. Objects are the parts of a program using which we organise different tasks that are meant to be achieved by the application. In OOP several modules and task are again organised using the objects.
Objects are integral part of any object oriented programming languages and as Java is OOP language it fully supports the concept of objects. But there are some other features of OOP.
Features of Object Oriented Programming (JAVA)
Any language which is object oriented has following set of features including Java:
1.     Abstraction: Abstraction means releasing only essential and useful features and hiding other unwanted feature or internal implementation. Abstraction is not a practical phenomenon but it is a purely logical concept. In OOP the phenomenon of Abstraction is achieved through the use of encapsulation, yet another feature of OOP.
Ex: To understand the concept of Abstraction let’s take an example of calculator. You are not aware of the internal implementation of calculator but still you can perform all the calculations required by and which are supported by the calculator. In this all the internal implementation or unnecessary details which are not required and are not meant to be for user are hidden but all the essential features only of calculations are exposed to user.
2.      Encapsulation: Encapsulation means the wrapping of internal implementation which includes functionality and data, as single unit. In Java one of the members of Java programming language ‘class’ is used to encapsulate the internal implementation (data and functionality).
3.      Polymorphism:  Polymorphism means performing multiple tasks of same type with same name. Polymorphism allows us to use the already written method more than once and thus decreases maintenance overhead.
Ex:  In a program of calculation area of different shapes like square, rectangle, circle etc we can use method with name as ‘CalculateArea’.
4.     Inheritance: Inheritance is the process by which we can reuse the features implemented in one class, for another class. By implementation we can reduce the number of lines in our code so it avoids time and maintenance problem also.
Ex: In a specific application we can define one class as ‘User’ and we can extend the features of this class to other class like ‘NormalUser’, ‘Editor’, ‘Administrator’ etc. Here some of the features like username and password can be extended newly defined classes like ‘NormalUser’, ‘Editor’, ‘Administrator’. In this case class ‘User’ is called as parent class and other newly defined classes which are inheriting the features of this class are called as child classes.
5.     Class: Class is very important concept in Java. Class acts as blue print for the object. It provides the structure and other support for the object. By using the structure defined in class we can define new members of the class which are called as objects. Class serve as data type for the object as keyword ‘int’ serve as data type for 10 or any other integer value. The class is defined type in Java and it is not available in other OOP languages.
In Java java.lang.Object will be parent for other classes by default.
How to define a class in java?
Like any other programming language java also has specific syntax to declare each member of Java programming language. So the syntax for declaring a class in java is:
[Modifier(s)] class [extends] implements[interface(s)]
6.     Object: An object is instance of class. We can create as many instances as we want (specifically required by application). Object is used to instantiate the class; without object class is nothing in a real application. We create reference in memory to store the entities which are defined in class. Thus class provides structure to create an entity and an object represents that particular entity in memory and in real time. Object has state and behaviour.
How to create an Object in java?
The syntax to create object in Java is:
=new
OR
new
In above declaration syntax the first represents the class for which we are going to create the object.
represents reference variable using which
 The new object will be stored in memory and it can be used to later to reference the object in different contexts.
Ex: This example shows how to create and class and object in Java:
Class  Human{             //This statement in Java is responsible for creating a class.
String name;
String nationality;
String gender;
int age;
}
Human h1=new Human();      //This statement in Java is responsible for creating a object of class Human.
So in above example we have just declares the class and created new member of or instance of class which will be represented by reference h1 in later part of the program. The ‘new’ keyword in above code snippet is defined keyword in Java and is used to create instances of class or a new object. This is not a complete Java program and this program just aims to provide you insight to the Object and class and how object and class are created in Java.
There some rules that every programmer should follow while writing the class and objects in java; but we will see those rules in later parts.


java.util package explained completely.

Sachin R Kukale - 02:16
In java java.util package is available. It is not default package in Java which means that you have to write import statement to use the features available in this pakcgae.
‘java.util’ package contains many utility classes, legacy classes and mainly collections framework.
The classes which have been introduced since the first version of Java are called as legacy classes.
The classes which are introduced from Java2 to manage the collection of elements/objects are collectively known as collection framework in Java.
Collection framework in Java is very important set of classes that lets you implement various data structures in java, effectively and easily.
To store the group of elements of same type you can use arrays but arrays have following limitations:
ü  You need to define the type of objects at the time declaration of array.
ü  You need to specify the size of array at the time declaration of array which can not be changed later.
ü  Insertion and deletion of elements in the middle positions of the array is not possible.
ü  You can not manage and access the elements of array effectively and easily.
To solve these problems and to handle the elements more effectively and easily following legacy classes are used in java:
            i.            Vector: Vector is a legacy class used to store objects or collections of elements without any key value or name value. You can collect all the objects in vector directly.
          ii.            Stack: Stack is also legacy class and it is used to stored objects, like vectors only.
        iii.            Hashtable: Can be used to store the objects or collections of objects as key and its corresponding name value.
        iv.            Dictionary: Can be used to store the objects or collections of objects as key and its corresponding name value.
          v.            Properties: Can be used to store the objects or collections of objects as key and its corresponding name value.
        vi.            Enumeration: Enumeration is interface defined in java.util package. The main purpose of using enumeration interface is to access the data stored in legacy classes one by one.
These legacy classes or collection framework has following advantages over arrays:
ü  You need not define the size of collection of objects, at the time of declaration. These legacy classes or collection framework uses the concept of capacity, which ensures the dynamic management of memory. Default capacity will be 10, if you are not defining the capacity initially.
ü  You need not to specify the data type of elements for which you are using the legacy classes or collections.
ü  You can access the elements of legacy classes or collections framework one by one so modification is easily possible.


Java programming language introduction.

Sachin R Kukale - 02:16
Java Programming Language: Java is strongly and statically typed, object oriented, class based, general purpose programming language.
By strongly and statically typed we mean that....
Programming language is set of rules defined to write the instructions for program which are intended for performing some specific task for machines or computers or any electronic devices like calculator.
Program is set of instructions which are written by using some specific programming language to perform some specific task.
Java is general purpose and high level programming language. By general purpose we mean that you can use Java programming language to develop some application but you cannot use it as research language like C or C++. By high level programming language we mean that Java language is not directly understood by computers or machines. To make Java understood by computers or machines we have to use some system software like Compiler and Interpreter.
In the computer science we have following types of languages:
1.     Machine Level Programming Languages: Machine level programming languages are those languages which are directly understood by the machines or computers. Since, machines can understand the languages of 0s and 1s; these languages use these two symbols only. Machine level programming languages are difficult to understand by the programmer as it is difficult to remember all the combinations of 0s and 1s to produce particular instructions. In early ages if computers when computers were not so common, such languages were being used.
2.     Assembly Language: Since machine languages were difficult to understand by humans; computer scientist developed programming languages by using mnemonics in English like language. Since humans or programmers can understand mnemonics (some specific words to define some particular action) it is easier to understand and to write programs in Assembly language. But computers were still unable to understand the assembly language directly so they developed system software called assembler which is responsible for converting the assembly language to the language understood by computers. So the assemblers are the program which converts assembly language code into machine code.
3.     High Level Language: But still assembly languages were not so easy to understand and program as it contained only small amount of words (mnemonics) which can be understood by the programmers. To solve this problem computer scientist developed some high level languages including C, C++ etc. High level machine language contains words from English language are used, which are easy to remember by the programmers. But computers cannot understand the high languages directly so to make it understood by the computers we use the concept of system software like Compiler or Interpreters. The task of both complier and interpreter is to convert high language into the language understood by the computers. The difference between compiler and interpreter is that compiler translates high level programming language to the language understood by the computer in one shot while the interpreter does the same job line by line.
So Java is high level programming language which uses compiler and interpreter to convert the java program into the machine code, how? We will see it later.


Best notes on interfaces in Java.

Sachin R Kukale - 02:14
Interface is custom type which can be defined by the user as and when need arises.
Why and when we create interface in java?
Uses of interface in Java.
You can define interface as a fully abstract type and even if you do not define it as ‘abstract’ interface will be always abstract internally.
You can declare ‘public static’ variables explicitly (by yourself) but even if you do not define them as ‘public abstract’ the variables inside the interface will be public abstract by default i.e. in case you do not use any modifiers while declaring variables. Following is the list of modifiers that can be used with variable declaration inside the interface:
·         Private.
·         Protected.
·         Transient.
·         Volatile.
When you declare any method inside the interface then it will be ‘public abstract’ by default. So you must terminate the method termination with semicolon (;) as it is ‘abstract’.
But you can define other types of methods in interface by using any of the following methods:
·         Private.
·         Protected.
·         Final.
·         Synchronized.
·         Native.
·         Strictfp.
Inside an interface you can not define the following blocks:
·         Constructors.
·         Instance block.
·         Static blocks.
You can declare an inner class inside the interface but it will be ‘public static’ inner class.
You cannot use interface directly but you must define an class which implements the interface, the features of which you want to use. But any class which is implementing an interface must implement all unimplemented methods which are not implemented in interface but are declared in the interface.
You can avoid this implementing all the methods in interface by declaring a class as ‘abstract’.
Interface or a class can ‘implement’ any number of other interfaces but an interface cannot ‘implement’ class. In other words an interface cannot inherit the features of a class. But either class or an interface can inherit features of one or more interfaces.
You can also create a variable for an interface which will be of type interface and it can hold the reference of Objects of classes which are implementing that particular interfaces.

There is another special case of interface called as marker interface. Marker interface does not have any implementation inside it and marker interface is used to verify the condition that whether the class can perform certain tasks or not.

Handling input/output im Java using io packeage.

Sachin R Kukale - 02:13
You may be required to read the data from input device or to write the data to output device. Input or output device will be treated as resources and they are managed by OS. So to interact with these devices we need to interact with OS.
We can also interact with input and output devices using the drivers but in that case you have to use the low level implementation which will platform as well as device independent and will lead to code maintenance problem as well.
To solve this problem java vendor has provided a package called io package for which implementation is provided by java vendor itself. The java.io package contains many classes and implementations that can be used effectively to interact with io devices. Also if you want to change the corresponding read or write device then you just have change the object related and all the process will be same.
JVM is started when we write ‘java...’ in consol and a object will be created by JVM to manage the io operations. The reference of this object will be stored in reference variable identified in System class.
Some references defined in system class to perform io operations are:
‘in’ –is reference which will specify the default input device.
‘out/err’ – will be the references for default output device.
You can change these devices by using the appropriate set method from system class if need arises.
While performing the io operations you can use the data as either as byte type or as character type.
The sequence of bits is called as ‘stream’ and we can either read or write the data in streams.
There are two types of stream depending upon the operation:
Input Stream and Output Stream.
And two types of stream depending on the type of data:
Byte stream and Character stream.
*      ByteInputStream: You can read the data from input using this stream as sequence of bytes.
·         java.io.InputStream: This is super class for err ByteInoutStream classes.
·         CharacterInputStream: By using this stream you can read the data from input devices as sequence of characters.
·         java.io.reader: This is super class for all character input stream classes.
To read the data from buffer you can use BufferedInputStream.
*    ByteOutputStream: This stream is used to write the data to the output devices from application. When you are using this stream you are sending the data as byte type.
·         java.io.OutputStream:  This is super class for character output stream classes.
·         CharacterOutputStream: Using this stream you can write the data to the output device as sequence of characters.
·         java.io.writer: This is super class for all character stream output classes.
Ø  All super classes in this package are abstract classes meaning that these superclasses do not have their own implementations.
Ø  There are other various sub classes which can be used for io operation.
Ø  Between input stream and reader class there is no inheritance relationship and you can use reader object to convert input stream into reader, if you have object of input stream. java.io.InputStreamReader class is subclass of reader and has constructor as input stream parameter.
Ø  In the same we you must remember that there is no inheritance relationship between output stream and writer class.
Ø  You can use the following class to convert output stream object into writer object:
java.io.OutptuStreamWriter clas.
Ø  While using the writer object for sending to the output device you need to invoke the flush() method to send the data to the output device.
You need to specify the device from where you are going to access the data.
You can use FileInputStream or FileReader to read the data from file:
FileInputStream(file f)
FileInoutStream(String fName)
File_Reader(file f)
File_Reader(String fname)
In the same way FileOutputStream and FileWriterClass are used to write the data to the file:
FileOutputStream (file f)
FileOutputStream (String fName)
FileOutputStream (file f,Boolean Operand)
FileOutputStream (String fName,Boolean operand)
FileWriter(file f)
FileWriter(String fName)
FileWriter(file f,Boolean operand)
To perform the operations on the file you can use java.io.file class.
You can also create constructor by using the following constructors:
public File(String fName)
public File(String parentDir,String fName)
public File(File parentDir,String fName)
Operations:
        i.            To create new file: public boolean CreateNewFile()
      ii.            To create one directory: public boolean mkdir()
    iii.            To create directories and sub-directories: public boolean mkdirs()
     iv.            To verify the file or directory: public boolean isFile()/public Boolean isDirectory()
       v.            To verify hidden file: public boolean isHidden()
     vi.            To get last modified information: public long last modified()
   vii.            To set up last modifier information: public void lastModified(long val)
 viii.            To verify file is readable or not: public boolean canRead()

     ix.            To verify file is writable or not: public boolean canWrite()

Inner classes and their types in Java.

Sachin R Kukale - 02:11
Inner class is a class which is defined inside another class in Java. Inner class is used to serve the following purposes in Java:
ü  To increase the logical grouping of classes based on some criteria.
ü  To access the private member of a class inside other class i.e. to increase encapsulation.
ü  To keep related classes together i.e. to increase the maintenance.
ü  To make the code more readable and maintainable.
Types of inner class are:
        i.            Static Inner class: A class which is declared inside another class with ‘static’ modifier are called as static inner class. It is static member for its outer class.
Such class can all the constructors and initialization blocks as its parent class.
You can access static inner only by referencing its outer class. For accessing the instance inner member of static inner class then you have to create the instance of static inner class using the outer class and then you can access instance inner member of static inner class.
You should access static inner class members using static inner class name (for static member) and with the instance of static class (for instance member). But in any case static inner class must be accessed using the name of its outer class.
      ii.            Instance Inner class:  A class which is declared without ‘static’ modifier inside another class is called as instance inner class. It serves as instance of its outer class.
To access the instance inner class you must create the instance of outer class and using that instance of outer class you can access instance inner class. In instance inner class only ‘static final’ variables are allowed to declare.
Accessing Instance Inner class:
a)      Inside outer class instance method:
To access the ‘static final’ variable of instance inner class you have to create the instance of inner class.
To access the instance member of instance inner class then you have to create instance of inner class without using the outer class instance.
b)      Outside outer class: To access the ‘final static’ member of instance inner class then you must provide names of inner class as well as outer class as:
..;
Ex: Student.FeeDetails.totalfee;
Here ‘FeeDetails’ is Instance inner class of ‘Student’ class and ‘totalfee’ is final static variable in ‘FeeDetails’.
For accessing instance member of Instance inner class:
ü  First of all, create instance of outer class.
ü  Using this instance of outer class create instance for inner class.
ü  Using this instance of inner class you can access instance member of inner class as:
  =();
. =.();
Ex:      
Student st=new Student();
            Student.FeeDetails fee=st.new FeeDetails();
    iii.            Local Inner class: Local inner class is class which is declared inside any method/constructor/block. The scope of local inner class inside the block in which it is declared and local inner class in accessible only in context in which it is declared i.e. inside method/constructor/block in which it is declared.
Only ‘final’ local variable of method/constructor/block can be accessed inside the local inner class.
With local inner class declaration you can use only two modifiers as: final or abstract.
You cannot create reference variable for local inner class outside the, method/constructor/block in which it is declared.
   iv.            Anonymous Inner class: Anonymous is a class without any class name. It must be subclass of other and it can be declared inside any other class or outside also. Anonymous class cannot members with ‘static’ modifiers.
It can be declared between the constructor call and semicolon.
Class file for the anonymous class will be generated as:
$
‘intvalue’ is value assigned by compiler to identify and manipulate the anonymous class internally and it depends on the textual order of such anonymous classes in source file.
Anonymous class can have only one instance and the reference of anonymous class should be stored in reference of its parent type
New members can be defined in anonymous class but those members should not be declared in parent class already.

Anonymous class can also be created with ‘interface’ as its super type.
Next Previous
Editor's Choice