Let's explore java

Monday, September 27, 2010

some more in super


Here is the example program using super keyword.     
Class add
{
double weidth,length,height;
add(add ob)
{
weidth =Ob.weidth;
length= Ob.length;
heigth= Ob.height;
}
add(double w,double l,double h)
{
weidth= w;
length= l;
heigth= h;
}
Double volume()
{
return weidth*length*height;
}
}
Class subtract extends add
{
Double bredth;
Subtract(double w,double l,double h,double b)
{
Super(w,l,h);
bredth=b;
}
}
Class demo
{
Public static void main(string args[])
{
Subtract ob1=new subtract(12,34,56,67.8);
add  ob2=new add();
Double vol;
Vol=ob1.volume();
System.out.println(“volume of it  :=”+vol);
Ob2=ob1; //assigning ob1 reference (of subclass) to the add reference which is super class
Vol=ob2.volume(); //this statement is ok, because volume is defined in add class.
System.out.println(“the volume is :”+vol);
System.out.println(“bredth of the ob1 is :”+ob2.bredth);

}
}
THE SECOND USE OF SUPER
The second form of super act somewhat like this, expect that it always reffers to the superclass of the sunclass in which it is being used. It has following format
Super.member
 Here member can be either method or an instant variable.
The example program based on this second use of super is as follows
Char a
{
int i;
}
Class b extends a
{
int i;
b(int a,int b)
{
super.i=a;
i=b;
}
Void show()
{
System.out.println(“the value of I in superclass is:”+super.i);
System.out.println(“the value of I in subclass is:”+i);
}
Class suuper
{
Static public void main(string args[])
{
b ob1=new b(12,45);
ob1.show()
}
}
MULTILEVEL HIERARCHY
Till now what we studied that one class inherites the properties of another in order to run successfully.Now we will see that there can be many classes which can be subclass for their above classes.we can tell those classes as multilevel classes. Let’s elaborate these points by some example programs.
class first
{
int i,j,k;
first(first  f)
{
i =f.i;
j=f.j;
k=f.k;
}
first(int a,int b,int c)
{
i=a;
j=b;
k=c;
}
void show()
{
System.out.println(“i=”+i+”j=”+j+”k=”+k);
}
class second extend first
{
int l;
second(second f)
{
super(f);
l=s.l;
}
second(int a)
{
super(I,j,k);
l=a;
}
class third extends second
{
int m;
third(third f)
{
super(f);
 m=t.m;
}
third(int a)
{
Super(i,j,k,l);
m=a;
}
class final
{
Public static void main(String args[])
{
third  ob1=new third(10,20,30,40,50);
 third ob2=new third(25,15,35,45,56);
ob1.show();
ob2.show();
}
}
Here we found that the class name called third is the subclass of second class which is ultimately the subclass of first class i.e third class can inherite the properties from both the classes.

Friday, September 24, 2010

INHERITANCE


INHERITANCE
Inheritance in class represents the ability of one class to inherits some of the useful properties from other classes.inheritance is the cornerstones in object oriented programming .The classes which are inherited are called SUPER CLASS and the one which inherites are called SUB CLASSES. For inheriting one class we simply incorporate the difinition of one class to the other by using extends keyword. It will be more clear from the simple example program.
Class a
{
Int I,j;
Void showij()
{
System.out.println(“I and j are:”+i+” “+j);
}
}
Class b extends a
{
Int k;
Void showk()
{
System.out.println(“the value of k is :”+k);
}
Void sum()
{
System.out.println(“i+j+k =”+(i+j+k));
}
}
Class inheritance
{
Public static void main(String args[])
{
a superob1=new a();
b subob2=new b();
superob1.i=12;
superob1.j=10;
superob.showij();
subob2.i=18;
subob2.j=12;
subob2.k=7;
subob2.showk();
subob2.sum();
}
}
Let’s discuss about some more practical example program
Class add
{
double weidth,length,height;
add(add ob)
{
weidth =Ob.weidth;
length= Ob.length;
heigth= Ob.height;
}
add(double w,double l,double h)
{
weidth= w;
length= l;
heigth= h;
}
Double volume()
{
return weidth*length*height;
}
}
Class subtract extends add
{
Double bredth;
Subtract(double w,double l,double h,double b)
{
weidth=w;
length=l;
height=h;
bredth=b;
}
}
Class demo
{
Public static void main(string args[])
{
Subtract ob1=new subtract(12,34,56,67.8);
Double vol;
Vol=ob1.volume();
System.out.println(“volume of it  :=”+vol);
}
}
A SUPERCLASS VARIABLE CAN REFERENCE A SUBCLASS OBJECT
A reference variable of a superclass can be assigned a reference object.this line might seem strange to you but I am sure within a few moment you will be happy by knowing this concept.For that let me explain the above example program with some modification.

Class add
{
double weidth,length,height;
add(add ob)
{
weidth =Ob.weidth;
length= Ob.length;
heigth= Ob.height;
}
add(double w,double l,double h)
{
weidth= w;
length= l;
heigth= h;
}
Double volume()
{
return weidth*length*height;
}
}
Class subtract extends add
{
Double bredth;
Subtract(double w,double l,double h,double b)
{
weidth=w;
length=l;
height=h;
bredth=b;
}
}
Class demo
{
Public static void main(string args[])
{
Subtract ob1=new subtract(12,34,56,67.8);
add  ob2=new add();
Double vol;
Vol=ob1.volume();
System.out.println(“volume of it  :=”+vol);
Ob2=ob1; //assigning ob1 reference (of subclass) to the add reference which is super class
Vol=ob2.volume(); //this statement is ok, because volume is defined in add class.
System.out.println(“the volume is :”+vol);
System.out.println(“bredth of the ob1 is :”+ob2.bredth); /*the following statement is invalide because ob2 does not define bredth member.*/

}
}
Here ob1 is the reference to the subtract object and ob2 is the reference to the add object.since subtract is the subclass of the add class so it is permissible to assign ob2 a reference to the subtract object.
                                                                               But we should also take into account that when a reference of a subclass object is passed into the reference of superclass object,you will have access only to those parts of the objects defined by the superclass.        This is why ob2 can’t access the bredth even it refers to subtract object.
Now I hope that it does not make your mind mad and you have understood what I wanted to tell.
In the preceding example program this problem arose `and you should be thinking that the subclass must be granted access to these members.However sometimes you will want to create the super class that keeps it’s all information to itself.In this case there will be no way for the subclass to directly access the members or initilize it.But in java we whenever a subclass want to refer to the superclass imidiately ,so these can be done by using super keywords

Monday, September 20, 2010

understanding static


UNDERSTANDING STATIC
There will be the time when we will need to define a class member that will be used independently of any objects of that class. Normally what we find that the methods inside the class can be called with the help of only objects of that class,However it is possible to create a methods or the members which can be called by itself only without the use of instances of that class. To create such member preceds it with the static keyword. We should always keep in mind that whenever the member is created under static then it can be called before the use of objects of that classes. We can declare both the member and the methods under static keyword.
               Whenever we use static keywords we should follow some rules as follows:=
They can only call other static methods.
They must only access only the static datas
They can not be refered to this or super at anyway
here we must see one simple example program to understand it in the most effective manner.
class usesatic
{
static int a=3;
static int b;
static void meth(int x)
{
System.out.println(“x=”+x);
System.out.println(“a=”+a);
System.out.println(“b=”+b);
}
static
{
System.out.println(“static block initialize”);
b=a*4;
}
public   static void main(String args[])
{
meth(42);
}
}
If we want to access the method outside the class where it has been declared,then we have to just use classname followed by the dot operators and followed by the method name
INTRODUCING FINAL
Sometimes we need a constant variable which can’t be modified further,and for doing so java introduced the concept of final keyword. With the help of this kwyword we can define any variable as a constant which can not be changed in later phase of the program
final int a=23;
final float b=34.56;
now we can see that the value of the a and b remains constant till the programs ends.it can not be changed to some other values.
ARRAY REVISITED
There is special array attributes that is used to find out how much elements an array can store. That attribute’s  name is length .let’s see how this can be used
//the program demonstrate the length array number
class Length
{
public static void main(atring args[])
{
int a[]=new int[10];
int a2[]={3,4,5,6,7,8,9,10};
int a3[]={23,34,45,56,7,8};
iystem.out.println(“length of a1 is”+a.length);
System.out.println(“length of a2 is”+a2.length);
System.out.println(‘length of a3 is”+a3.lenght);
}
}
Here what we see that object.length gives us the total size that the specific array can hold upto.
Let’s peep into some more example to clarify our doubts
/*Improved stack class that uses the length array number*/
Class stack
{
Private int stak[];
Private int tos;
//allocate and initialize the stak
Stack(int size)
{
Stak=new int [size];
tos=-1;
}
//push an item onto the stack
void push(int item)
{
If(stak.length-1)
System.out.println(“stack is full”);
Else
Stak[++tos]=item;
}
//pop an item from the stack
Int  pop()
{
If(tos<0)
System.out.println(“the stack is underflow”);
return 0;
}
else
return stak[tos--];
}
}
Class teststak2
{
Public static void main(String args[])
{
Stack mystack1=new stack(5);
Stack mystack2=new stack(8);
//push some number onto the stack
for(int i=0;i<5;i++)
mystack1.push(i);
for(int i=0;i<8;i++)
mystack2.push(i);
//pop those number from the stack
For(int i=0;i<5;i++);
System.out.println(“stack in mystack1 is”+mystack.pop());
}
}



Friday, September 17, 2010

Argue with argument


Argument passing
Arguments in the java program can be called in two different ways.
1st one is call-by value.this approach copies the values of an argument into the formal parameter of the subordinate.therefore the change made to the parameter has no effect in the actual argument.the second way an argument can be passed is the call-by reference. The reference to the argument is passed to the parameter.that means the changes made to the parameter automatically changes the value of the argument. This ligic can be cleared with the help of one simple program.
class test
{
Void meth(int I,int j)
{
I*=2;
j/=2;
}
}
Class callbyvalue
{
Public static void main(String args[])
{
Test ob=new test();
Int a=15,b=20;
System.out.println(“a and b before call”+a+” “+b);
Ob.meth(a,b);
System.out.println(“a and b after call”+a+” “+b);
}
}
As we can see that the operation that occurs inside the meth() method have no effect on the value of a and b used in the call.
But reverse of this call-by reference  we should always keep in mind that when we create a variable of the class type we are only creating a reference to the object.thus when we pass this reference to the method,the parameters that receives itwill refer to the same objects as that refered by the argument.
Now we should take a look into one simple program to illustrate the following.
class test
{
Int a,b;
test(int i.intj)
{
a=I;
b=j;
}
//pass an object
void  meth(test 0)
0.a*=2;
0.b/=2;
}
}
Calss callbyref
{
Public static void main(String args[])
{
Test ob=new test(15,20);
Syatem.out.println(‘ob.a and ob.b before call is:”+ob.a+” :”+ob.b);
Ob.meth(ob);
System.out.println(“ob.a and ob.b after the call is:”+ob.a+” :”+ob.b);
}
}
Returning objects
A method can return any type of the data,including calss types that you create.let’s have a quick look into one simple program
Class test
{
Int a;
Test(int i)
{
a=I;
}
Test increbyten()
{
Test temp=new test(a+10);
Return temp;
}
}
Class retob
{
Public static void main(String args[])
{
Test ob1=new test(2);
Test ob2;
Ob2=ob1.increbyten();
System.out.println(“ob1.a=:”+ob1.a);
System.out.println(“ob2.a=:”+ob2.a);
Ob2=ob2.increbyten(“ob2.a=:”+ob2.a);
}
}
As we can see here each time increbyten() is invoked,a new object is created,and a reference to it is returned to the calleing routine.
Recursion
We can define recursion as it is the attribute which allows the method to call itself. The method which call itself is said to be recursive.
The classic example of recursive is the compution of the factorial of a number.
//a simple example of recursion
Class factorial
{
//this is the recursive method
Int fact(int n)
{
Int result;
If(n==1)
Return 1;
Result=fact(n-1)*n;
Return result;
}
}
Class recursive
{
Public static void main(String args[])
{
Factorial f=new factorial();
System.out.println(“factorial of the 3 is”+f.fact(3));
System.out.println(“factorinal of the 4 is”+f.fact(4));
}
}
Here is how it works.when fact()is called with an argument 1,the function returns 1.otherwise it returns the result of product of fact(n-1)*n;
INTRODUCING ACCESS CONTROL
Through encapsulation we can control what part of the program can access the member of the class. By controlling access we can prevent the misuse of the variables and methods in java.
Java’s access specifiers are the public,private,and protected.one has to keep in mind that protected is applied when inheritance is being called.
When the member of the class is modified by the public specifier ,then that member can be accessed by any other codes.when the member of the class are specified by the private then that member can only be accessed by the other member of that class.

Let’s have a look on one sample program to illustrate  the following

//this program demonstrate the difference between the private and public
Class test
{
Int a;//default access
Public int b;//public access
Private c;//private access
//methods to access c
Void sets(int i)
{
C=I;
}
Int Gtc()
{
Return c;
}
}
Class accestest
{
Public static void main(Static args[])
{
Test ob1=new test;
//these are ok….a and b may be accessed directly
Ob.a=10;
ob.b=210
//this will cause error
Ob.c=100;
//you must access  c through its methods…