tag:blogger.com,1999:blog-59336463944200685792024-03-05T23:06:54.957+05:30JUMP IN JAVACODE TO THE CORE!!!ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-5933646394420068579.post-80826749914591510912013-01-06T21:17:00.000+05:302013-01-06T21:17:13.402+05:30<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<b><u><span style="font-size: large;">DIFFERENCE BETWEEN TOP-DOWN AND BOTTOM-UP APPROACH</span></u></b></div>
<div style="text-align: justify;">
<b><u><span style="font-size: large;"><br /></span></u></b></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b>TOP-DOWN APPROACH:</b></span></div>
<div style="text-align: justify;">
<b><u><span style="font-size: large;"><br /></span></u></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">In Top-down approach, first, the main function is designed and progresses down towards the lowest level subsystems. When designing brand new system, Top-down approach is followed. Its advantage is that, it is easy to visualize, provide sense of completeness and easy to understand the progress at any stage.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Disadvantage includes, it may not help much in unit testing and there're chances of redundant business logics.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Here, an overview of the system is first formulated, specifying but not detailing any 1st level subsystems. Each subsystem is then redefined in yet greater detail, sometimes in many additional subsystem levels, until the entire specification is reduced to base elements.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Procedural programming languages like C tend towards top-down as it is started with a function and adding up to it.</span></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b>BOTTOM-UP APPROACH:</b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b><br /></b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">In Bottom-up approach, lowest level modules or subsystems are first developed and progresses upwards to the main function. It is used in Reverse Engineering process which requires analysing someone's design. Its advantage is strong business logic, ability to write good unit testing code and modifications can be performed easily. Its disadvantage is that it requires a lot of effort in writing test cases and programs can't be verified easily in the mid-stage.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Here, the individual base elements of the system are first specified in great detail. These elements are then linked to form larger subsystem.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">OOP(Eg: C++, Java) tend to start from bottom, growing up, as it is begun with objects.</span></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The reality is seldom one or the other approach but a combination of both.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Thanks for reading my blog... :-) Do comment about my blog... ;-)</span></div>
</div>
ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.com0tag:blogger.com,1999:blog-5933646394420068579.post-90555258391065137232013-01-06T18:20:00.001+05:302013-01-06T18:20:44.721+05:30<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<b><u><span style="font-size: large;">WHAT DOES THE KEYWORD 'STATIC' MEAN???</span></u></b></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">In simple terms, a static variable/method can be accessed without the use of an object. There'll be only one copy of static variable/method for all the objects. This facility will be useful in several situations. For example, a static variable say count in library management program can count the no. of users visited on each day. </span></div>
<div style="text-align: justify;">
<b><span style="font-size: large;"><br /></span></b></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">POINTS TO BE REMEMBERED:</span></b></div>
<div style="text-align: justify;">
<b><span style="font-size: large;"><br /></span></b></div>
<br />
<span style="font-size: large;"><div style="text-align: justify;">
1) Whenever a class is loaded, its static statements are run first. </div>
</span><div style="text-align: left;">
<div style="text-align: justify;">
<b style="font-size: x-large;">EG:</b></div>
<span style="font-size: large;"><div style="text-align: justify;">
class call</div>
<div style="text-align: justify;">
{</div>
<div style="text-align: justify;">
int non_static=10;</div>
<div style="text-align: justify;">
static int var3=5,var4;</div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
static //STATIC BLOCK WILL RUN FIRST</div>
<div style="text-align: justify;">
{</div>
<div style="text-align: justify;">
System.out.println("Inside static block");</div>
<div style="text-align: justify;">
var4=25;</div>
<div style="text-align: justify;">
System.out.println("var3="+var3+"\t"+"var4="+var4);</div>
<div style="text-align: justify;">
}</div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
void print()</div>
<div style="text-align: justify;">
{</div>
<div style="text-align: justify;">
System.out.println(non_static);</div>
<div style="text-align: justify;">
}</div>
</span></div>
<div style="text-align: left;">
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<span style="font-size: large;"><div style="text-align: justify;">
public static void main(String a[])</div>
<div style="text-align: justify;">
{</div>
<div style="text-align: justify;">
call obj=new call();</div>
<div style="text-align: justify;">
obj.print();</div>
<div style="text-align: justify;">
}</div>
<div style="text-align: justify;">
}</div>
<div style="text-align: justify;">
<br /></div>
</span></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">OUTPUT:</span></b></div>
<div style="text-align: left;">
<div style="text-align: justify;">
<span style="font-size: large;">Inside static block</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">var3=5<span class="Apple-tab-span" style="white-space: pre;"> </span>var4=25</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">10</span></div>
</div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">2)Only one copy of static variable/method will be available in a class, which all the objects use.</span></div>
<div class="separator" style="clear: both; text-align: justify;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ht4VxafpHy9YvnreEsfDr5udb0KNDgq59wKdQ-jHC1xp3mgKU_k8FD-XXczL_1P_3Wfjt0mt9WQh-QwSB9rdWX-MTkFahqFQOVfhNYOU5ixYglKkS3nQqimHEHHo-uCVqhBGvdv2GYs/s1600/static.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: large;"><img border="0" height="152" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ht4VxafpHy9YvnreEsfDr5udb0KNDgq59wKdQ-jHC1xp3mgKU_k8FD-XXczL_1P_3Wfjt0mt9WQh-QwSB9rdWX-MTkFahqFQOVfhNYOU5ixYglKkS3nQqimHEHHo-uCVqhBGvdv2GYs/s400/static.jpg" width="400" /></span></a></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">3)Static methods can call only other static methods.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">4)Static methods must access only static data. This means that, nonstatic variables which are </span><span style="font-size: large;">already declared</span><span style="font-size: large;"> outside the static method</span><span style="font-size: large;"> can't be accessed inside static method. But it is possible to declare nonstatic variable inside static methods and use it.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">5)They can't refer to 'this' or 'super' keyword. (Since these keywords are related to objects).</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b>EG:</b></span></div>
<div style="text-align: left;">
<span style="font-size: large;"><div style="text-align: justify;">
public class Static </div>
<div style="text-align: justify;">
{</div>
<div style="text-align: justify;">
int non_static=10;</div>
<div style="text-align: justify;">
static int var1=5,var2;</div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
void nonstatic_method()</div>
<div style="text-align: justify;">
{</div>
<div style="text-align: justify;">
System.out.println("this is nonstatic_method");</div>
<div style="text-align: justify;">
System.out.println("var1="+var1);</div>
<div style="text-align: justify;">
//in nonstatic methods, both static and nonstatic variables can be used.</div>
<div style="text-align: justify;">
}</div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
static void static_method()</div>
<div style="text-align: justify;">
{</div>
<div style="text-align: justify;">
System.out.println("this is static_method");</div>
<div style="text-align: justify;">
System.out.println("non_static variable="+non_static); </div>
<div style="text-align: justify;">
//ERROR! since this method is static, it can't use non static variables</div>
<div style="text-align: justify;">
System.out.println("var1="+var1);</div>
<div style="text-align: justify;">
System.out.println("var2="+var2);</div>
<div style="text-align: justify;">
nonstatic_method();</div>
<div style="text-align: justify;">
//This is again an error since static method can call only other static methods</div>
<div style="text-align: justify;">
}</div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
public static void main(String a[])</div>
<div style="text-align: justify;">
{</div>
<div style="text-align: justify;">
Static obj=new Static();</div>
<div style="text-align: justify;">
obj.nonstatic_method(); </div>
<div style="text-align: justify;">
//Object is mandatory to call nonstatic methods</div>
<div style="text-align: justify;">
static_method();</div>
<div style="text-align: justify;">
//static methods can be called without using object.</div>
<div style="text-align: justify;">
}</div>
<div style="text-align: justify;">
}</div>
<div style="text-align: justify;">
<b><br /></b></div>
<div style="text-align: justify;">
<b>OUTPUT:</b></div>
<div>
<div style="text-align: justify;">
this is nonstatic_method</div>
<div style="text-align: justify;">
var1=5</div>
<div style="text-align: justify;">
this is static_method</div>
<div style="text-align: justify;">
var1=5</div>
<div style="text-align: justify;">
var2=0</div>
</div>
</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">6)In the above example, static members are accessed from within the class in which they're declared. But when static members are to be accessed from another class, it must be done using the class name to specify which class' static member is accessed. For example, consider a class test which needs to access static variable var1 or static_method() of the previous eg.,</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: left;">
<div style="text-align: justify;">
<span style="font-size: large;">class test</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> public static void main(String a[])</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> Static.var1=6; //CLASS NAME IS MANDATORY HERE</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> Static.static_method();</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b><br /></b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b>OUTPUT:</b></span></div>
<div>
<span style="font-size: large;"><div style="text-align: justify;">
this is static_method</div>
<div style="text-align: justify;">
var1=6</div>
<div style="text-align: justify;">
var2=0</div>
</span></div>
</div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">7)And the last point is, note that we write as</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> <b>public static void main(String str[])</b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">and not as </span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> public void main(String str[])</span></div>
<div style="text-align: left;">
<div style="text-align: justify;">
<span style="font-size: large;">The reason behind is that, main() function is the starting point of a program. In java, main() is a part of a class and hence an object is required to access it. But when the program starts, JVM needs to call main() for which objects are not available. This is the reason we add </span><b style="font-size: x-large;">static </b><span style="font-size: large;">which indicates that main() function can be called even without an object.</span></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<span style="font-size: large;">Thanks for reading my blog... :-) Hope it is useful... Put your response in the comment... ;-)</span></div>
</div>
</div>
ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.com0tag:blogger.com,1999:blog-5933646394420068579.post-17060488052880950402012-09-30T23:39:00.003+05:302012-09-30T23:39:32.783+05:30<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;"><b>DIFFERENCE BETWEEN next() AND nextLine()</b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">This post eradicates the confusion between next() and nextLine() and gives you a clear idea of how they work.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">next() and nextLine() methods are associated with Scanner and is used for getting String inputs. Their differences are...</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">next() can read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b>COMMON PROBLEMS WITH next() :</b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b><br /></b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Consider the example,</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<span style="font-size: large;"></span><br />
<br />
<div style="text-align: justify;">
<span style="font-size: large;">import java.util.Scanner;</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">public class temp</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> public static void main(String arg[])</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> Scanner sc=new Scanner(System.in);</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("enter string for c");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> String c=sc.next();</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("c is "+c);</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("enter string for d");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> String d=sc.next();</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("d is "+d);</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;"><b><br /></b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b>OUTPUT: HOW IT WORKS!</b></span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;">enter string for c </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">abc def abc |def //cursor stays after c</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">c is abc</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">enter string for d</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">d is def abc def| //cursor stays after f</span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Look at the output keenly. When the input for c is given as <b>abc def, </b>it will not read the entire input <b>abc def. </b>next() reads only abc and places the cursor after the alphabet c. Now, when value for d is got using next(), it will not get new value. Rather, it takes the value after the current cursor position(since the cursor is after c, def is assigned to d and the cursor stays after f).</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now, when </span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b> String c=sc.next()</b></span><b style="font-size: x-large;"> </b><span style="font-size: large;">in the above program is replaced as </span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> <b>String c=sc.nextLine(),</b> the output will be</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b>OUTPUT: HOW IT WORKS!</b></span></div>
<span style="font-size: large;"></span><br />
<div style="text-align: justify;">
<span style="font-size: large;">enter string for c</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">ABC DEF ABC DEF</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">c is ABC DEF | //cursor moves to next line</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">enter string for d</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">GHI ABC DEF</span></div>
<span style="font-size: large;"><div style="text-align: justify;">
d is GHI GHI| //cursor stays after I</div>
</span><span style="font-size: large;"><div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Since nextLine() is used for reading input for c, the entire line is read and hence ABC DEF is stored in c and the cursor is placed in the next line. Now when d is read using next() or nextLine(), the cursor will be in the next line and hence the input for d is read and is placed in d (here d gets the input GHI).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
So whenever next() is used, make sure nextLine() used to place the cursor in the next line. Hence the above program is also written as, </div>
<div style="text-align: justify;">
<b>String c=sc.next();</b></div>
<div style="text-align: justify;">
<b>sc.nextLine();</b></div>
<div style="text-align: justify;">
<b>String d=sc.next();</b></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Thanks for reading my blog... :-) Comment about my blog. Stay tuned for my next post.... </div>
</span><br />
<span style="font-size: large;"><div style="text-align: justify;">
<br /></div>
</span></div>
ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.com39tag:blogger.com,1999:blog-5933646394420068579.post-86119203999994821042012-09-30T19:19:00.000+05:302012-09-30T19:19:37.280+05:30<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;"><b><u>INTERFACE</u></b></span><br />
<span style="font-size: large;"><b><u><br /></u></b></span>
<span style="font-size: large;"><b><u>CAN A CLASS IMPLEMENT 2 INTERFACES HAVING COMMON METHOD NAME???</u></b> </span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">We know that a class can extend only one class(since multiple inheritance is not supported) and implement any number of interfaces. Consider a situation where a class implements 2 interfaces and both the interface have one method in common. </span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"><u>Consider an eg:</u></span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"></span><br />
<span style="font-size: large;">interface inter1</span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;"> void print();</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;">interface inter2</span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;"> void print();</span><br />
<span style="font-size: large;"> void increment();</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;">public class psvm implements inter1,inter2</span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;"> int test=10;</span><br />
<span style="font-size: large;"> </span><br />
<span style="font-size: large;"> @Override //OPTIONAL</span><br />
<span style="font-size: large;"> public void print()</span><br />
<span style="font-size: large;"> {</span><br />
<span style="font-size: large;"> System.out.println("hai in print()");</span><br />
<span style="font-size: large;"> }</span><br />
<span style="font-size: large;"> </span><br />
<span style="font-size: large;"> public void increment()</span><br />
<span style="font-size: large;"> {</span><br />
<span style="font-size: large;"> System.out.println(++test);</span><br />
<span style="font-size: large;"> }</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"> void other_methods()</span><br />
<span style="font-size: large;"> {</span><br />
<span style="font-size: large;"> //Method Body</span><br />
<span style="font-size: large;"> }</span><br />
<span style="font-size: large;"> public static void main(String args[])</span><br />
<span style="font-size: large;"> {</span><br />
<span style="font-size: large;"> System.out.println("hai");</span><br />
<span style="font-size: large;"> psvm object=new psvm();</span><br />
<span style="font-size: large;"> object.print();</span><br />
<span style="font-size: large;"> object.increment();</span><br />
<span style="font-size: large;"> }</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;"><br /></span><span style="font-size: large;">this is possible... the implementing class will have the method common for two interfaces which will satisfy the requirements of both the interfaces(requirements in the sense, if an interface is implemented by a class, then the class must use all the methods given in the interface). Hence it doesn't matter what type of reference variable is used to call the common method. </span><br />
<br />
<span style="font-size: large;">To make my statement sooo elementary, let me tell you like this. You promise your friend Thomas that you'll wear red shirt tomorrow. You also promise your friend Jack that you'll wear red shirt. Is it necessary to put two red shirts??? !!!! :-D One red shirt will satisfy the promise made to your friends. The same is applied here.</span><br />
<br />
<span style="font-size: large;">And note that void print() is prefixed with the keyword <b>public.</b> This is needed to distinguish the print() method from the other normal methods (eg: void other_methods() ) of the class. The keyword public indicates the compiler that the function print() has some declaration before its use (here it indicates that print() is in the interface). Hence <b>public void print() </b>is used rather than mere void print().</span><br />
<span style="font-size: large;"><br /></span><span style="font-size: large;">Also it is not necessary that all the methods in the interface must be used once a class implements an interface. It may not use some of the methods specified in the interface. In such case, the class name must be prefixed with the keyword <b>abstract.</b></span><br />
<span style="font-size: large;"><b><br /></b></span>
<span style="font-size: large;">For eg.</span><br />
<span style="font-size: large;">Suppose if we develop a class named xxx which implements inter2 and if class xxx uses only the print() method, then it must be written as</span><br />
<span style="font-size: large;"><b>abstract class xxx implements inter2</b></span><br />
<span style="font-size: large;">rather than</span><br />
<span style="font-size: large;">class xxx implements inter2</span><span style="font-size: large;"></span><span style="font-size: large;">This kind of implementation of interface is often called as <b>partial implementations.</b></span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">And finally, since interface does not contain definition of the methods, an interface can't implement another interface but it can extend other interfaces. It can extend multiple interfaces too.</span><br />
<span style="font-size: large;">Eg:</span><br />
<span style="font-size: large;">interface A</span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;"> method1();</span><br />
<span style="font-size: large;"> method2();</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;">interface B</span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;"> method3();</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;">interface C extends A,B</span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;"> method4();</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">This is the speciality of an interface, since classes can't extend more than one class but interface can extend one or more interfaces... ;-)</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">Thanks for reading my blog :-) positive comments are invited. Stay tuned for my next blog... ;-)</span><br />
<br /></div>
ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.com2tag:blogger.com,1999:blog-5933646394420068579.post-1671114363583507102012-09-22T23:29:00.002+05:302012-09-22T23:29:57.601+05:30<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<b><u><span style="font-size: large;">CAN TWO CLASSES HAVE A FUNCTION WITH SAME SIGNATURE WITHOUT INTERFACE?</span></u></b></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Consider the program...</span></div>
<div style="text-align: justify;">
<br /></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;">class A</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> void print()</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("hai in class A");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">class B</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> void print()</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("hai in class B");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">public class y_interface</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> public static void main(String arg[])</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> A obj=new A();</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> obj.print();</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> B obj1=new B();</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> obj1.print();</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> </span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Here class A and class B have a function <b>void print()</b></span><b style="font-size: x-large;"> </b><span style="font-size: large;">with same signature. The above program runs without any error. Its output is</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b><br /></b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b>OUTPUT:</b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">hai in class A</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">hai in class B</span></div>
<span style="font-size: large;"></span><br />
<div style="text-align: justify;">
<span style="font-size: large;">The main intention of this blog is that, there may be confusion like it is not possible to have two classes having function with same signature without interface. The main goal of introducing interface concept in Java is to provide support for multiple inheritance and for polymorphism(i.e. ability to perform same operation on a variety of objects). This blog clears this particular doubt.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Thanks for reading :-) Comment about the blog. Queries are highly welcomed. Stay tuned for my next blog ;-)</span></div>
<br />
</div>
ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.com0tag:blogger.com,1999:blog-5933646394420068579.post-88087000025406479042012-09-13T22:06:00.000+05:302012-09-22T23:35:40.186+05:30<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<br /></div>
<div style="text-align: justify;">
<b><u><span style="font-size: large;">IS IT POSSIBLE TO EXTEND THE CLASS WHICH HAS MAIN FUNCTION???</span></u></b></div>
<div style="text-align: justify;">
<b><u><span style="font-size: large;"><br /></span></u></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">Yes, it is possible to extend the class which has main function. When i tried it using notepad and command prompt, i got the expected output. In NetBeans, i got a message...</span></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">The file has more main classes.</span></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">and it asked me to chose the main class from the list. When i choose the super class which has main(), i got the correct output. Also the output is correct when i choose the subclass. But i think it is stupidous to extend a class having main(). I didn't find any answer in Google for this question and i think this kind of inheritance has no real application. </span><span style="font-size: large;">The program which i tried is...</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"></span></div>
<span style="font-size: large;">public class psvm_class </span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;"> int a=5;</span><br />
<span style="font-size: large;"> </span><br />
<span style="font-size: large;"> public static void main(String args[])</span><br />
<span style="font-size: large;"> {</span><br />
<span style="font-size: large;"> System.out.println("hai in main()"); </span><br />
<span style="font-size: large;"> demo obj=new demo(); </span><br />
<span style="font-size: large;"> obj.display(); </span><br />
<span style="font-size: large;"> }</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;">class demo extends psvm_class</span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;"> void display()</span><br />
<span style="font-size: large;"> {</span><br />
<span style="font-size: large;"> System.out.println("hai in demo class");</span><br />
<span style="font-size: large;"> System.out.println("value of a is "+a);</span><br />
<span style="font-size: large;"> }</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"><b><u>OUTPUT:</u></b></span><br />
<br />
<span style="font-size: large;">hai in main()</span><br />
<span style="font-size: large;">hai in demo class</span><br />
<span style="font-size: large;">value of a is 5</span><br />
<br />
<span style="font-size: large;">The above program has two main() functions in it, one which is in psvm_class. Since class demo inherits from psvm_class, it also has one copy of main() !!!!! But still the program runs without any error!!!!!</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">Discussions and ideas about this peculiar case are invited... Post your ideas and comments about this blog. Sometimes i may be wrong. Your ideas might help me in correcting this blog. Feel free to spot it out. Thanks for reading this blog... ;-) Stay tuned for my next blog...</span><br />
<span style="font-size: large;"><br /></span>
</div>
ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.com3tag:blogger.com,1999:blog-5933646394420068579.post-68369445525912946302012-09-05T21:56:00.001+05:302012-09-22T23:35:31.964+05:30<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<br /></div>
<div style="text-align: justify;">
<u><b><span style="font-size: large;">CAN I HAVE THE CLASS CONTAINING MAIN FUNCTION ANYWHERE IN THE PROGRAM?</span></b></u></div>
<div style="text-align: justify;">
<u><b><span style="font-size: large;"><br /></span></b></u></div>
<div style="text-align: justify;">
<span style="font-size: large;">It is not mandatory that a class which has main() must be at last. It can be anywhere. The program given below is an example...</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><b><u>EG:</u></b>
</span></div>
<br />
<br />
<div style="text-align: justify;">
<span style="font-size: large;">public class psvm_class </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> static void print()</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("hai in print()");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> public static void main(String args[])</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("hai in main()");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> print(); //STATIC MEMBER OF SAME CLASS</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> //display(); THIS WILL THROW AN ERROR</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> demo.display(); //STATIC MEMBER OF DIFFERENT CLASS</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("a is "+demo.a);</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;">class demo</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> static int a=0;</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> static void display()</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("hai in demo class");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<br />
<br />
<div style="text-align: justify;">
<b><u><span style="font-size: large;"><br /></span></u></b></div>
<div style="text-align: justify;">
<b><u><span style="font-size: large;">OUTPUT</span></u></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">hai in main()</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">hai in print()</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">hai in demo class</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">a is 0</span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<b><u><span style="font-size: large;">CAN I CALL STATIC MEMBERS JUST BY ITS NAME IF IT IS NOT PRESENT IN CURRENT CLASS???</span></u></b></div>
<div style="text-align: justify;">
<b><u><span style="font-size: large;"><br /></span></u></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">It is well known that static members doesn't need any object for accessing it. If static members are present in the class from where it is called, then we can simply use its name for accessing it. But if it is in different class, we can't simply use its name. In this case, it is necessary to use the class name to specify that the static member which we are calling is in the class mentioned while calling. In the above example class demo has a static data member 'a' and static method display(). When they are accessed in main() without class name, an error is thrown like...</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;"><b>Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: display </b><b>at extend_psvm_class.main(extend_psvm_class.java:12)</b></span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Hence the correct way of accessing is </span></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">demo.a</span></b></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">demo.display()</span></b></div>
<div style="text-align: justify; text-decoration: underline;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">But the static members of the class psvm_class can be called just by their name as</span></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">print()</span></b></div>
<br />
<div style="text-align: justify;">
<b><u><span style="font-size: large;"><br /></span></u></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">A question may arise like why class name is used for accessing static members in different classes. This is because it is possible to have another class with static display() say...</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;">class demo1</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> static void display()</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("hai in demo1");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now in this case, if we simply use </span></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">display()</span></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">then compiler doesn't know which display() method to call. To avoid this ambiguity, whenever static members of different classes are accessed, they are done by their corresponding class name.</span></div>
<br />
<br />
<br />
<div style="text-align: justify;">
<b><u><br /></u></b></div>
</div>
ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.com2tag:blogger.com,1999:blog-5933646394420068579.post-28867890346856802012-09-03T22:09:00.000+05:302012-09-22T23:35:18.769+05:30<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<div style="text-align: center;">
<br /></div>
<span style="font-size: large;"><b><u>CAN WE CALL A METHOD IN A CLASS WITHOUT AN OBJECT FROM MAIN FUNCTION WHICH IS DEFINED IN THE SAME CLASS???</u></b></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Consider a class, say, class A, with one attribute and one method. main() is also inside the class A. Most of us may think since main() is in class A, we can access data members and methods without using an object. But this is not the case. You require an object to access the class members. </span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<u><span style="font-size: large;">Eg:</span></u></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;">public class psvm</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> int test=10;</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> </span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> void display()</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("hai in display()");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> </span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> public static void main(String args[])</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> {</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> System.out.println("hai");</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> display(); //THIS WILL THROW AN ERROR!</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"> }</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">}</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">For the above program, you will get an error like...</span><br />
<span style="font-size: large;"><b>non-static method display() cannot be referenced from a static context</b></span><br />
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">But if the display method is declared static as</span></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">static void display()</span></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">then there will not be any error because static members does not require an object for invocation.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Also note keenly that the above program does not have import section but still the above program runs without any error. The reason is that the default package is </span></div>
<div style="text-align: justify;">
<b><span style="font-size: large;">java.lang.*</span></b></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The reason why this package is made default is, coding a java program becomes useless without much functionality in java.lang because it is in this package, the basic data types like byte, string, int, thread etc are present. Hence this package is made as default.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<br /></div>
</div>
ramhttp://www.blogger.com/profile/04013083062229955943noreply@blogger.com4