Monday, 31 March 2014

Event Handling

An event generally occurs when some thing happened on a Graphical user interface component.

For Example
User can click the button, then mouse click event happened on the button.
User can select a check box, then selection event happened on the check box.

Events may also occurs that are not directly caused by the interactions with user interface.

For Example
An Event may be generated when counter exceeds a value, timer expires etc.,

Event Source
The Graphical component generating the event is called Event source. Whenever an event occurs, the Event Source sends an event object, which contains the information about the event to the listeners.

Event Listener
A listener is an object that is notified when an event occurs. The event object gets processed by an event listener


Prevoius                                                 Next                                                 Home

Thursday, 27 March 2014

private modifier

Any data, methods and type which is declared as private can't accessible from outside the class.

You can't make class as private, There is no point in making the class as private, A class that no other class can see, and therefore can't be used for anything.

But Java supports private nested classes and inner classes.




Prevoius                                                 Next                                                 Home

default : no modifier

The data members, methods, types(includes classes, interfaces etc.,) which are not defined with any access specifier like private, protected and public all are come under default category.

The default data accessed with in the package, not outside.

package data_structure.linear;

public interface DataStructure{
 int MAX_SIZE = 1000;
 
 void display();
 void insert(int data);
 void delete();
 int search(int data);
}


package data_structure.linear;

public class Stack implements DataStructure{
 int top = -1;
 
 int stack[] = new int[DataStructure.MAX_SIZE];
 
 int getTop(){
  return top;
 }
 
 public void display(){
  for(int i=0; i<=top; i++){
   System.out.println(stack[i]);
  }
 }
 
 public void insert(int data){
  if(top < DataStructure.MAX_SIZE-1){
   top++;
   stack[top] = data;
  }
  else{
   System.out.println("Stack is not empty");
  }
 }
 
 public void delete(){
  if(top == -1){
   System.out.println("Stack is Empty");
  }
  else{
   stack[top] = 0;
   top--;
  }
 }
 
 public int search(int data){
  for(int i=0; i < top; i++)
   if(stack[i] == data)
    return 0;
  return -1;
 }
}


package data_structure;

import data_structure.linear.*;

class StackTest{

 public static void main(String args[]){
  Stack myStack = new Stack();
  
  myStack.delete();
  
  myStack.insert(10);
  myStack.insert(20);
  
  myStack.display();
  System.out.println(myStack.getTop());
 }
}

When you tries to compile StackTest.java, compiler throws the below error






Prevoius                                                 Next                                                 Home

protected access specifier


Above diagram shows the directory structure.
  1. Directory named “data_structure” contains two sub directories linear and non-linear.
  2. Directory linear contains two java files named Stack.java, DataStructure.java
Any member which is declared as protected is accessible with in the package and subclasses of a class.


package data_structure.linear;

public interface DataStructure{
 int MAX_SIZE = 1000;
 
 void display();
 void insert(int data);
 void delete();
 int search(int data);
}



package data_structure.linear;

public class Stack implements DataStructure{
 int top = -1;
 
 int stack[] = new int[DataStructure.MAX_SIZE];
 
 protected int getTop(){
  return top;
 }
 
 public void display(){
  for(int i=0; i<=top; i++){
   System.out.println(stack[i]);
  }
 }
 
 public void insert(int data){
  if(top < DataStructure.MAX_SIZE-1){
   top++;
   stack[top] = data;
  }
  else{
   System.out.println("Stack is not empty");
  }
 }
 
 public void delete(){
  if(top == -1){
   System.out.println("Stack is Empty");
  }
  else{
   stack[top] = 0;
   top--;
  }
 }
 
 public int search(int data){
  for(int i=0; i < top; i++)
   if(stack[i] == data)
    return 0;
  return -1;
 }
}


package data_structure;

import data_structure.linear.*;
class StackTest{
 public static void main(String args[]){
  Stack myStack1 = new Stack();
  
  myStack1.delete();
  
  myStack1.insert(10);
  myStack1.insert(20);
  
  myStack1.display();
  System.out.println(myStack1.getTop());
 }
}

Statements to execute the StackTest.java

C:\>javac -cp . data_structure\linear\DataStructure.java

C:\>javac -cp . data_structure\linear\Stack.java

C:\>javac -cp . data_structure\StackTest.java

C:\>java -cp . data_structure.StackTest
data_structure\StackTest.java:14: error: getTop() has protected access in Stack
System.out.println(myStack1.getTop());
^
1 error

As you observe the output, compiler throws the error, since getTop is protected, so it can accessible from within the package or the subclasses of the Stack class.

To make the program runs fine make the StackTest class to extend the Stack class.


package data_structure;

import data_structure.linear.*;

class StackTest extends Stack{

 public static void main(String args[]){
  StackTest myStack1 = new StackTest();
  
  myStack1.delete();
  
  myStack1.insert(10);
  myStack1.insert(20);
  
  myStack1.display();
  System.out.println(myStack1.getTop());
 }
}
Output
C:\>javac -cp . data_structure\StackTest.java

C:\>java -cp . data_structure.StackTest
Stack is Empty
10
20
1

Some Points to Remember
1. Java don't allow protected classes and protected interfaces
Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses.
Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing. But Java supports protected nested or inner classes


Prevoius                                                 Next                                                 Home

public access specifier



Above diagram shows the directory structure.
1. Directory named “data_structure” contains two sub directories linear and non-linear. 
2.Directory linear contains two java files named Stack.java, DataStructure.java


package data_structure.linear;

public interface DataStructure{
 int MAX_SIZE = 1000;
 
 void display();
 void insert(int data);
 void delete();
 int search(int data);
}


package data_structure.linear;

public class Stack implements DataStructure{
 int top = -1;
 
 int stack[] = new int[DataStructure.MAX_SIZE];
 
 public void display(){
  for(int i=0; i<=top; i++){
   System.out.println(stack[i]);
  }
 }
 
 public void insert(int data){
  if(top < DataStructure.MAX_SIZE-1){
   top++;
   stack[top] = data;
  }
  else{
   System.out.println("Stack is not empty");
  }
 }
 
 public void delete(){
  if(top == -1){
   System.out.println("Stack is Empty");
  }
  else{
   stack[top] = 0;
   top--;
  }
 }
 
 public int search(int data){
  for(int i=0; i < top; i++)
   if(stack[i] == data)
    return 0;
  return -1;
 }
}

Lets say StackTest.java is in the package data_structure.

package data_structure;

import data_structure.linear.*;
class StackTest{
 public static void main(String args[]){
  DataStructure myStack1 = new Stack();
  
  myStack1.delete();
  
  myStack1.insert(10);
  myStack1.insert(20);
  
  myStack1.display();
 }
}




Statements to execute the StackTest.java
C:\>javac -cp . data_structure\linear\DataStructure.java

C:\>javac -cp . data_structure\linear\Stack.java

C:\>javac -cp . data_structure\StackTest.java

C:\>java -cp . data_structure.StackTest
Stack is Empty
10
20


 
As You observe the Stack.java, It is a public class and all the methods in the stack class are public, So from StackTest.java which is completely outside of the package “linear” we accessed the Stack class and and its public methods.




Prevoius                                                 Next                                                 Home

Wednesday, 26 March 2014

Access Specifiers

Java Supports 4 access specifiers: public, private, protected and default(no modifier). If program not specifies any access modifier, then default assumed.

Modifier Class Package subclass Global
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

Public data is accessible from any where i.e, from within the package or outside the package

protected data accessible from within the package and the sub classes of the class.

Data with no modifiers accessible from with in the package.

Private data accessible from with in the class.


Prevoius                                                 Next                                                 Home

Static import

All the static members are accessed by using the ClassName. By using static import we can call the variable or method with name itself, no need to call the members with ClassName as prefix.

Without using static import
import java.lang.Math;
class WithOutStaticImport{
 public static void main(String args[]){
  System.out.println("Square root of 100 is " + Math.sqrt(100));
 }
}

Output
Square root of 100 is 10.0

With static import
import static java.lang.Math.*;
class WithStaticImport{
 public static void main(String args[]){
  System.out.println("Square root of 100 is " + sqrt(100));
 }
}

Output
Square root of 100 is 10.0



Prevoius                                                 Next                                                 Home