Tuesday, September 3, 2013

Encapsulation | how to achieve encapsulation in Java


Encapsulation:


Before moving into the concept and definition, first of all i would like to ask you one thing. Do you know about capsule (medicine)? Have you seen capsule .?

Everyone has the same answer for the above questions. that is YES

If you know a capsule , definitely you knew about encapsulation.

How a capsule will look like?

As every one knows that the medicine which is wrapped is the capsule.

How Capsule related to Encapsulation: 


Just like a capsule(which wraps up medicine) , wrapping up  the data is called Encapsulation in OOPS. Encapsulation is a mechanism of hiding the data 
( variables/methods ) from the external world.

In java a class is the best example of Encapsulation

How to achieve Encapsulation in Java:


In java, Encapsulation can be achieved by using access specifiers (especially private modifier). 

As we know a private access specifier specifies access with in the class (local access), the data can't be exposed to the other classes (in same application or different application).


Let me elaborate this with an example.

CapsulateData is the class with some variables as given below.



As accountNumber, accountName are private variables they can't be accessed outside the class.

If we try to do so, following will be the result.





Here as accountName and accountNumber are private fileds, we can't access them in other classes(not only variables , even private methods also).

From this, we can clearly understood that the data
 (accountName , accountNumber ) is hidden by the class from the outside world, which we consider to be encapsulation.




Monday, September 2, 2013

Abstraction in OOPS | How Abstraction can be achieved in JAVA


Abstraction:


We all know that abstraction is one of the characteristics of Object Oriented Programming System (OOPS). If you ask someone "what is abstraction?" the most common possible answer that you hear is .. "Abstraction is hiding some important features or data ..bla ..bla..bla "

The question is ..what is hiding ..? where you are hiding..? which part you are hiding ..?

Is hiding something is the correct definition for abstraction ?

then what it (abstraction) really means?

Here it goes:


In simple words i can say.. "Abstraction implies providing something.. but not everything"

i will consider one real time example to illustrate it in much better way.




Suppose there is an aeroplane flying in the sky. We know that it is flying, but we don't know how exactly it is flying and what are the underlying mechanisms, working and all. 

this is what we are looking for.. "abstraction"

How to achieve Abstraction in Java:


By using abstract classes or interfaces , we can achieve abstraction in Java.
As we all know that abstract classes, interfaces contains abstract methods (ofcourse abstract classes may or may not contain) i.e , only method declarations.

By looking at the method declaration we can estimate what exactly the method is doing, what it is going to return. But we don't know how exactly the abstract method will be implemented.

We came to know about the implementation, once we provided the method implementation in the classes which implement the corresponding abstract class or interface.

Example for the above illustration:


Let us consider an interface ( you can take abstract class also) Addicted.




In the above interface, getAddictedListOfPersons() is the abstract method (by default all the methods in an interface are public abstract ) which returns list of all the addicted persons. By looking at the method declaration we can infer this.
(As per the java documentation we should give proper names to the methods and variables)

But we don't know how exactly the method will be implemented in it's (interface) implementation classes.

Following are the implementation classes for the interface Addicted.


The class AddictedToJava is providing implementation to the abstract method which returns the list of persons who were addicted to Java.



The class AddictedToAndroid is providing implementation to the abstract method which returns the list of persons who were addicted to Android.


Now , by looking at these implementations we came to know that the implementation is different in both of the implementation classes for the interface(Addicted).

Only after providing implementation, we came to know how exactly the method was implemented.

This is considered to be abstraction what i mentioned in the begining of the article.

Abstraction ------> Providing something.. but not everything  





Thursday, May 30, 2013

Reading cookies from browser using core java

Cookie:

A cookie is a piece of information sent by server(web program) to client(browser).

A cookie will have a name and a value which will be stored in browser's cache.
There are two types of cookies available.

--> Persistent cookies and 
--> Non Persistent cookies

Persistent cookies will be stored in browser's cache even though we have closed the browser until some time specified in the web program in which they got created.

Non Persistent cookies will be removed from the browser's cache whenever we close the browser.

In JSE edition, java.net package provides the feature of reading cookies from browser.

This can be illustrated as shown.

CookieManager will manages all the cookie related tasks and will be helpful in getting cookies.

As part of CookieManager interface we have some final static fields available to set the cookie policy.

Content policy describes the acceptance state of cookies. i.e, which cookies to be accepted and which should be rejected.

The cookie policy has predefined policies namely ACCEPT_ALL, ACCEPT_NONE and ACCEPT_ORIGINAL_SERVER

ACCEPT_ALL is to accept all cookies , ACCEPT_NONE  is to reject all cookies and ACCEPT_ORIGINAL_SERVER is to accept cookies from original server.




A sample program to read the cookies stored in browser with java net package is as shown.


Once if you run the application, CookieManager will set the cookie policy to ACCEPT_ALL so that it can accept any cookie. A CookieHandler is used by the http protocol and can be registered by using setDefault().

Once we have opened the connection to the specified URL by using CookieManager we can get the CookieStore which contains a bunch of cookies related to the url. 

By using CookieStore we can get the list of cookies with their name and values

The output of the application will display list of cookies in the browser related to the url with cookie name and value.