Let's explore java

Friday, October 01, 2010

concept of inheritence


METHOD OVERRIDINiG
Whenever the method(functions) in the subclass has the same name and type signaure as that of super class then those methods are said to be method overriding.when those methods are called then the always shows the output related to the subclasses.
One example program will make you understand easily.
Class a
{
Int I,j;
a(int a,int b)
{
I=a;
J=b;
}
Void show()
{
System.out.println(“i=” +i+ “j=” +j);
}
Class b  extends a
{
Int k;
B(int a)
{
Super(i,j);
K=a;
}
Void show()
{
System.out.println(“k=”+k);
}
Class c
{
Public static void main(String args[])
{
B ob1=new b(1,2,3);
Ob1.show();
}
}
After getting output what we will find that the method overridind has given the output which represents k.it overrites the value of I and j,so this is only called method overriting.
                       If you want to access the super class verson of overriting method ,you can do it by using super.
Here is the example program related to the problem
Class a
{
Int I,j;
a(int a,int b)
{
I=a;
J=b;
}
Void show()
{
System.out.println(“i=” +i+ “j=” +j);
}
Class b  extends a
{
Int k;
B(int a)
{
Super(i,j);
K=a;
}
Void show()
{
System.out.println(“k=”+k);
}
Class c
{
Public static void main(String args[])
{
B ob1=new b(1,2,3);
Super.show();
Ob1.show();
}
}
With the help of this program we can be able to get the output from super class also.
Dynamic method dispatch
In the earlier virson of programming we show that in method overriding the method called by subclass are override the method of superclass and now we will learn that this method overriding forms the basis of java’s most powerful concept “dynamic method dispatch”.this is the machnism by which a call to method overriding is resolved at run time and not at compile time.
                                                                         First of all it should be known to you that a superclass reference variable can refer to the subclass object.java uses this fact to resolve the call to overriding method at run time.If a superclass contain the method which is being overriding by subclasses then the different types of objects are refered through the superclass reference variables.
One example related to this concept with satisfy your doubt.
Class A
{
Void callme()
{
System.out.println(“inside a’s callme method”);
}
}
Class B extends A
{
Void callme()
{
System.out.println(“inside b’s callme method”);
}
}
Class C extends A
{
Void callme()
{
System.out.println(“inside c’s callme method”);
}
}
Class dispatch
{
Public static void main(string args[])
{
A=a;
B=b;
C=c;
A=r;
R=a;
r.callme();
r=b;
r.callme();
r=c;
r.callme();
}
}


 

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());
}
}