Thursday, July 30, 2009

Microsoft and Google JV to Target Google search Market

Microsoft and Yahoo signed a 10 year pact to outsource the search technologies to beat the search engine market leader Google.Under this pact Microsoft will provide the underlying search technology on Yahoo's popular websites.The deal provides a lift for the recent overhaul of its search engine Bing.Bing has already won praises and favourable reviews after falling behind Google.Running such a search system proves expensive ,and Microsoft can now filter more searches through the Bing technology infrastructure.Bing expects to deliver better answers by learning through a series of user queries.

The term of 10 years agreement calls for Microsoft to license the Yahoo's search technologies in return Yahoo will receive a lucrative 88% of search generated revenue initially.The workload for advertising will be split.Yahoo on one hand will focus on premium advertisers whereas on the other hand Microsoft will focus on medium and small advertisers.The decision of advertising rates will be solely through the auction.

Based on study the fact came out that Google own's 78% marketshare in search engines.Combining both Yahoo and Microsoft will never be close to it.But they can aim at that to increase the share by providing accurate searches.The revenue is completely monopolised by the Google and no match between them.

In a statement the Microsoft CEO Steven A Ballmer said,"Through this agreement with Yahoo ,we will create more innovation in search ,better value for advertisers and real consumer choice in the market currently dominated by single comp[any".

Even united,Microsoft and Yahoo will continue to be dwarfed by Google in search.Yet the combination of Microsoft and Yahoo in online search creates a far powerful counterweight to Google,one that will be welcomed by many in the advertising industry,which has watched Google rapidly becoming the world's largest seller for advertising.For Microsoft the combination with Yahoo is the quickest way to increase use of its nearly revamped and restructured search engine Bing.

Are you Intelligent!!!! Check
Get your SCJP kit now free!!!

Read more...

Tuesday, July 28, 2009

Untold truths about Sun Certified Java Programmer(SCJP)

Being a Sun Certified Java Programmer(SCJP) is undoubtedly a matter a proud,But due to immense increase in qualified Human Resource especially in IT/ITES sector, it is no more a matter of proud.Once upon a time(2-3 years back) we know that if anyone is Sun Certified or holds a Sun Certification he will surely get a good job which will give him opportunity to earn as well as learn and grow in person.There are many types of certifications are provided by Sun likewise SCJP,SCJA,SCWCD and so on.Which are developed to test one's knowledge and skills in particular field of specialization. What I clearly want to mention is if you do something do it as nobody else can do.That is if you are preparing for SCJP then you must qualify the exam with atleast 80% marks.Otherwise it will be useless.I have a case for you, I know a guy who is SUN Certified (SCJP) he works in a private firm and only gets Rs. 12,000/- Per Month ($ 600).You may think it is too less but it is reality here in India.He scored only 64% marks in SCJP exam.In current economic scenario and increasing competition take such students to work for less and that's pity about India.

Now come to the Examination part.Most of guys must know that India is a corrupt country.Let me tell you it is not the whole reality it is framed by media and persons who cannot see India grow.Still it is major factor.I also heard somewhere that here you can pass the exam by spending some extra money.You will get whole bunch of papers and questions will be asked from that.I tried to find out and it was around the cost of the coupon that you buy for the exam.This is pathetic and true injustice with the hardworking students.This way the peoples are just degrading the value of the Sun Certificate,nothing else.

You might be living in any part of the world but the silly thing is that you will never get a value for your thing.Like you will never get a expected job according to your qualifications.

I have another case for you that one of my friend knows nothing about programming and still managed to get a job in a giant IT company @ Rs. 21,000/- per month($ 1,050),almost 1.75 times the person who has qualified the SCJP.when I look at them I need to think well its luck or may be something else,maybe he's not able to show up his skills or anything.In terms of qualification its really unfair.

If you are outside India you may find yourself lucky that there are not that much Sun Certified programmers but India is a place full of certified programmers.It doesn't matter whether you are an Indian or not ,but looking forward to SCJP.Then keep pulling weights even more,otherwise you will be just another SCJP.

Most important thing never get out of touch.That is keep revising and testing your concepts with new ideas and codes.This way you will keep away from being rusted and at the same time when you will get a call for interview you will turn up positively,full of confidence.Big boys know how important confidence is in Interviews.

I am not discouraging you from giving SCJP but I want you to know the ground reality of SCJP.It would be better if you try for Cisco Certification ,Oracle Certification etc.Their demand is much higher than SCJP.It is the market trend.SCJP is a bit saturated field.You have to be a bit different that other Sun certification holders.
All the best ;-]

SCJP Resources:-

Get your SCJP preparation kit now!!!!

SCJP best ebook download
SCJP best exam preparation kit download
SCJP exam passing tips and tricks
SCJP exam objective.

Read more...

Thursday, July 23, 2009

SCJP Sun Certified Java Programmer best Ebook Free Download

Download SCJP5 ebook for free.The chapters listed here are taken from the latest book of SCJP 5 written by Catherine Sierra and Bert Bates.This book is considered as best book for the preparation of SCJP.Catherine Sierra has been a renowned author who has written many books in Java including the most selling Big Java.This ebook is regarded as best in world for the preparation of SCJP Sun Certified Java Programmer.This is No.1 selling in Amazon.com.I am providing these links because I want study should be of low cost in fact free. It is a good book for those who has some basic knowledge of Java but want to learn other core concepts.To download the SCJP5 ebook Catherine Sierra and Bert Bates click on following Links.The links are given in Chapter wise order as it makes easy to study,and download.You can bookmark this page and come back when you need particular chapter for study.

Free Download Links:-
*Disclaimer these links does not belong to the author.These are found over Internet and author is not responsible for them.

Read more...

Variable Shadowing Complexities Can you figure out??

There are many ways so that we can shadow our variables.One way is to hide an instance variable by shadowing it by local variable.Shadowing happens when we redecalre a variable that has already been declared somewhere else in the program.The effect of shadowing is to hide certain variables in such a way that it may look as though you are using the hidden variable,but you are actually using shadowed variable.

Typically it happens by accidents which returns some hard to find bugs,and there are reasons where you wish to use variable shadowing.

I assume you have no knowledge of Shadowing previously that's why here is one example followed by a practice question but it will be in different may:-

Example:-Shadowing instance variable with a local variable.

class dimensions{
static int height=20;
static void changeHeight(int height){
height=height+400;
System.out.println("height in changeHeight "+height);
}

public static void main(String a[]){
dimension dim=new dimension();
System.out.println("height before changeHeight"+height);
changeHeight(height);
System.out.println("height after changeHeight"+height);
}
}

When you will execute the above code the output would be:-

height before changeHeight 20
height in changeHeight 420
height after changeHeight 20

It is quite simple to understand since this is a call by value method call we call method changeHeight() by passing the copy of height instance variable.Since call by value passes value in bits thus local variable gets all the value of instance variable and thus all the operations are done on local variable rather than instance variable and instance variable remains untouched.

That was quite simple though but solve the following its quite messy.I will appreciate if you solve this and write the solution as a comment.

class MyNumber{
int number=44;
}

class ChangeNumber{
MyNumber mynum=MyNumber();
void changeNumber(MyNumber mynum){
mynum.number=88;
System.out.println("mynum.number in changeNumber is "+mynum.number);
mynum=new MyNumber();
mynum.number=99;
System.out.println("mynum.number in changeNumber is now "+mynum.number);
}
public static void main(String a[]){
ChangeNumber chgnum=new ChangeNumber();
System.out.println("chgnum.mynum.number in changeNumber is "+chgnum.mynum.number);
chgnum.changeNumber(chgnum.mynum);
System.out.println("chgnum.mynum.number after changeNumber is "+chgnum.mynum.number);
}
}

What would be its output???
Think and answer.Post answer in comments section.

Read more...

Tuesday, July 21, 2009

Interface legal declaration rules and issues

Interfaces follow certain rules which you need to know in order to become an Java expert.There are two rules mainly for interfaces implementation and declaration.

1.A class can implement more than one interface.That is the following declaration is perfectly legal:-

public class Subaru implements Colorable,speedable{// Horrible code here}

It is evident that you can extend only one class ,but you can implement many interfaces.The implemented interface describes the role the class will play.

2.An Interface can extend other Interfaces,but never implement anything.
Following is perfectly legal:-

interface Colorable extends Paintable{ ... }

This requires some thinking from you for a moment.Ok,lets see what happens in this case,here the first concrete (non abstract) implementation class of Colorable must implement all the methods from Colorable and also we have to implement all the buck of Paintable interface.

IMPORTANT:-An Interface can extend more than one Interface.

whoa!! this is strange we have never seen a class who can extend more than one class but what a mess Interface does this.It needs severe attention ...

For example :-

interface Colorable extends Paintable,Sprayble,Movable{ ... } // Perfectly legal

public class Subaru implements Colorable { ... }

The more interfaces the implemented interface will extend more messy it will for first concrete implementation of the interface.That is it has to implement all the methods of Colorable and also of all other extended interfaces.In this case of Paintable,Movable,Sprayble.

It doesn't matter whether you provide some thing inside implemented methods.You can left the implemented methods empty.

If the first implementation is an abstract class then the first concrete subclass of abstract class has to do all the work,implementation of all the methods.

Other Interface tutorials:-

Interface declaration rules
Interface rules and explanations
Why Java not support multiple inheritance

Read more...

Monday, July 20, 2009

Interfaces:rules and explanations

Interfaces are indeed the most important part of the Java OO paradigm.It enables Java to indirectly implement Multiple inheritance.In order to implement an Interface you have to adhere to certain rules defined for its implementation.Interfaces are implemented using "implements" keyword.

Example:-

interface Car_attributes{
void accelerate();
void color();
}

The main point to note here is that whenever you implement an interface you have to implement all the methods described in the interface.

Example:-

public class Subaru implements Car_attributes{
public void accelerate(){// Code}
public void color(){// Code}
public void gears(){// Code}
}

Here in above example Subaru class implements Car_attributes interface then Subaru has to mention all methods of Car_attributes interface.Otherwise compiler will not hesitate in slapping you with an error.But still world is full of exceptions and Interfaces are not a superhero(I hope you have watched Superhero Movie).We will look at those exceptions in a few moments.

The implementation classes must adhere to the some rules and these applies to the non abstract implementation:-
1.Provide non abstract(concrete) implementation for all the methods of the interface.
2.Follow all rules of legal overriding.
3.Maintain the signature of the interface method,and also maintain the same return types.
4.You need not declare the exceptions declared in the interface method declaration.
5.Declare no checked exception on implementation methods other than those declared by the interface method ,or subclass of those declared by the interface method.

Wait wait!! there comes the exception that is "if implementation class is abstract class then the abstract class will pass all its work to its first concrete subclass ".
Lets take a look at following example:-

**Suppose bounceable interface has a method bounce()

abstract class ball implements bounceable{}

class Football extends bounceable{
public void bounce(){// Code}
}

That is since Ball the implementing an interface and it is an abstract class then the sub class of Ball will implement all the methods declared in bounceable interface.

Interface Other Tutorials:-

Interface declaration rules
Interface legal declaration rules and issues
Why Java not support multiple inheritance

Read more...

Wednesday, July 15, 2009

Adding Records in database JDBC

In our applications most of the times we need to store the records and values in our database.Java provides ways to add records and data in database through JDBC.First of all you need to know the basics of Java.There are two types of JDBC Statements that can be used for addition of record in database.

1.Statement
2.PreparedStatement

We will see how we can add records in database through both Statement types.

1.Through Statement object:-using Statement class object we can run our static queries.Suppose if we want to add a fixed record in database each time query is processed,then only we can use Statement object.

For Example:-
public void addRecord(){
try{
stat.executeUpdate("insert employee values('X',20,2000)");
}catch(Exception e){}
}

Here in this case we are bound to use one query time the method add Record() is executed.This results in addition of same record in the database.This can prove to be a restriction on any application's functionality.
For solving above problem we use PreparedStatement object.Through PreparedStatement object we can pass values during runtime and make our application dynamic.This method also uses concept of Placeholders on which the value is being passed.

Example:-
*assumptions required

public void addRecord(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:MyData","","");
PreparedStatement pstat=con.createStatement("insert employee value(?,?,?)");
pstat.setInt(1,20);
pstat.setInt(1,200);
pstat.setInt(1,2000);
pstat.executeUpdate();
}catch(Exception e){}
}

NOTE:-you can set values by taking input from text boxes dynamically like following:-
//pstat.setInt(1,text1.getText());

Suggested Reading and Links:-
JDBC:how to delete a record
JDBC:PreparedStatement Object
JDBC:Statement Object
JDBC:ResultSet Object
querying database
connecting to database
Configuring data source
JDBC :Introduction



Read more...

Tuesday, July 14, 2009

SCJP best Exam preperation kit,Best SCJP quiz engine for free Download

The SCJP 310-065 exam is not easy to crack.I have an experience of using the UCertify's SCJP 310-065 exam preparation kit for quite a long time,and it is needed the best exam preparation kit available.I have also tried several other SCJP preparations kits buts they never worth money I have spent on them.UCertify offers a great SCJP 310-065 exam preparations kit which has a lot of features to consider and also as nothing is perfect a few drawbacks lets take a look:-

Features of UCertify SCJP exam preparation kit:-

1.Very good collection of qulaity questions.which you can expect in the exam.
2.Tests in the preparation kit are timed tests.
3.Clearly mentioned exam objectives.
4.Exajm specific notes.
5.Practice questions with every exam objective notes.
6.You can create your own custom tests timed or not.
7.There is also an interactive tests which adds interactivity in testing your skills.
8.Nine high quality timed tests to test your caliber.
9.Tips to success small little tips but are beneficial in huge way.
10.Articles present to make basic concepts very clear.
11.There is a final test which you can take finally if you think you studied enough to take the exam.
12.The quiz engine is a learning machine which keeps on storing your past scores and let you compare your previous performances to the current one.
13.You can check out if you are ready for the exam by checking the exam readiness report.
14.UCertify exam preparation is available for free.
15.About 700 high quality certification questions covering all objectives.

Shortcomings of UCertify SCJP exam preparation kit:-

1.The detailed description is not given in the notes.For that you have to refer some book.
2.Study notes which includes objective specific questions are too simple.

All the above features I think are good enough to try out the UCertify SCJP exam preparation guide.The advantages clearly outweighs the shortcomings.
There is much more You can try the quiz engine for free to download click below:-


Limited Period Offer:-

Special offer for Java World Readers get 10% off on every product you buy from the UCertify By just mentioning the Coupon Code "UCPREP".What's more all products of UCertify comes with a money back guarantee.If you think that the product is not good enough to let you crack the exam you can take your money back.

Read more...

Sunday, July 12, 2009

IS-A and HAS-A relationship :Every thing JAVA is in Relationship

Java represents two types of relationships:-


1.IS-A relationship
2.HAS-A relationship

IS-A Relationship:-In object oriented programming,the concept of IS-A is a totally based on Inheritance(extends) and Interface implementation(implements).It is just like saying "A is a B type of thing".For example ,5-Star is a chocolate,Subaru Impreza is a car etc.
It is key point to note that you can easily identify the IS-A relationship .Wherever you see an extends keyword or implements keyword in a class declaration,then this class is said to be passed IS-A relationship.

For Example:-

class Car{
// lots of complex work
}

class Impreza extends Car{
//Impreza extends and thus inherit all methods from Car (except final and static)
//Impreza can also define all his specific functionality
}

From the above code we can say "Car is a Vehicle","Impreza is a Car", this in turn reflects a transitive relationship that "Impreza is a Vehicle".

The relationship can be shown as below:-
Arrow direction from subclass to super class


HAS-A Relationship:-HAS-A relationship has nothing to do with Inheritance rather it is based on the usage of various variables and methods of other class.We can say "A HAS-A B if the code in class A has reference to an instance of B".Make it more clear look at following example:-

public class Car{}

class Impreza extends Car{
private Subaru_feature subaru_features;
}

As you can see Impreza HAS-A instance variable of type Subaru.That is we can say "Impreza HAS-A Subaru_feature ".Simply here Impreza has a reference type of Subaru_feature.Impreza can use the reference variable to invoke the subaru features without caring about the Subaru_feature code.HAS-A relationship is indicated by following figure:-


HAS-A relationship makes each and every class a specialist class.Making specialists classes has numerous benefits including reduction in bugs,easily tractable errors,Less complex code,reduction in code redundancy and most of code is easily understandable.The more specialist a class is the code reuse is even better.Its a good Object Oriented practice to use HAS-A relationship.

Suggested links:-

Home
complete OO tutorials
Inheritance complete explanation
Why Java not support multiple Inheritance?
Why code reuse is an exclusive feature of Inheritance.
Polymorphism:Core of Object Oriented Programming

Read more...

Saturday, July 11, 2009

Inheritance complete explanation

Inheritance is key concept in Java.It is almost everywhere in Java.It is nearly impossible to write the tiniest Java program without Inheritance.We all create classes in Java programs.Whenever we create a Java class we automatically inherit all of class object methods.


Inheritance has two key concepts:-
1.Code Reuse
2.Polymorphism

Inheritance means someone or something derives a set of attributes and properties from someone or something else,like lucky inherited diabetes from his father,Craig inherited his father's business.

Inheritance is one of the ways of implementing abstraction.As we know that abstraction is all about describing the essential detail and ignoring non-essential details.

Note:-

1.When a class inherits a set of attributes and behaviours from another class ,then class that inherits is called subclass and class from which attributes and behaviours are inherited is called super class.

2.An abstract superclass is a conceptual class which does not contain any code in particular but acts as a base from which other class inherits properties.

3.When a class inherits from two or more superclasses then it is be showing multiple inheritance.

Complete Example:-

Look at above figure ,In the given hierarchy,we have classes named Mammals,Dogs,Humans,Cats,Lions,Tigers,Leopards .Mammals class has following properties:-
1.They are vertebrates
2.They are warm blooded
3.They have hairs on their body

From the hierarchy it is clear that Dogs,Cats and Humans also have these characteristics too.In technical terms we can say that above mentioned classes have inherited these attributes from Mammal class.

The description given above does not contain any vertebral nature of skeletal system,it being warm blooded,its ears,hairs on the body and other features have already been described for the class Mammals and since cats inherit these features from Mammals,only the details specific to the cats need to mentioned in its class.

Suggested Links:-

Object Oriented Concepts
Why Java Not support multiple Inheritance

Read more...

Thursday, July 9, 2009

Using Tabbed Panes in Java Swings

Tabbed panes are just like property sheets in windows environment.Using tabbed pane you can create multiple panels that are visible in a single area.A Tabbed pane consist of series of Tabs.These tabs are present on the basis of categories of information or application they contain.A tabbed pane enables you to display information in a small area in a more effective manner.The JTabbedPane class of javax.swing package is used to create Tabbed panes.Tabbed panes are effective way to use the window area.Tabbed Panes looks like following:-


Tabbed Pane source code:-

import javax.swing.*;
import java.awt.*;

public class Tabbedpane extends JFrame{
public static void main(String a[]){

//creating a container JFrame
JFrame frm=new JFrame("Tabbed Pane");

//Creating Tabbedpane
JTabbedPane jtpn=new JTabbedPane();
JPanel color=new JPanel();

//adding panel to a tabbed pane
jtpn.addTab("Color",null,color,"Color info");
color.add(new JLabel("The color is not set yet"));
JPanel sound=new JPanel();

//adding panel to a tabbed pane
jtpn.addTab("Sound",null,sound,"Sound info");
sound.add(new JLabel("The sound is off"));

//adding component to the container
frm.getContentPane().add(jtpn);
frm.setBounds(200,200,200,200);
frm.show();

}
}

JTabbedPane class is used to ceate JTabbedPane oblect.
For adding a JPanel to a JTabbedPane method named addTab() is used.
Four parameters of addTab() method are:-

1.Label for Tab.
2.Image(if any otherwise null).
3.Component to be added to the Tabbed pane.
4.ToolTip for the tab.

Home

Read more...

Wednesday, July 8, 2009

Adding Border in Swing Application componenets

The Border class is in javax.swing.border package.Make sure you have imported the package before using borders in your application.You can add borders to components by using setBorder() method.This method has been declared in JComponent class and is available for all the components.Method getBorder() gives the name of border for the component.Borders add look and feel in your components.

BorderFactory class is used to create borders for a component.BorderFactory class has been declared in javax.swing package and contains method for creating various types of borders.

You can use following borders:-
  • Line Border
  • Etched Border
  • Bevel Border
Example for Line Border:-
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.BorderFactory.*;

public class bordertest extends JFrame{

public bordertest(){

JFrame frm=new JFrame("Border Test");
JButton button=new JButton("Line Border");
Border line=BorderFactory.createLineBorder(new Color(0,0,0),3);
button.setBorder(line);
frm.getContentPane().add(button);
frm.show();
frm.setSize(200,100);
}
public static void main(String a[]){
new bordertest();
}
}

createLineBorder() method is used to create a black line border that is three pixels wide.The setBorder() method is used to set the line border for the button.

Etched Border:-Replace the above code by following code from line 6.

/*
JButton button=new JButton("Etched Border");
Border etched=BorderFactory.createEtchedBorder();
button.setBorder(etched);
*/

createEtchedBorder() method is used to create Etched Border.


Bevel Border:-Bevel Border are of two types raised and lowered.You need to specify in the method createBevelBorder() which border you want.

Bevel Border Lowered:-Replace the line Border code by following code from line 6.

/*
JButton button=new JButton("Bevel Border");
Border bevellower=BorderFactory.createBevelBorder(BevelBorder.LOWERED);
button.setBorder(bevellower);
*/


Bevel Border Raised:-Replace the line Border code by following code from line 6.

/*
JButton button=new JButton("Bevel Border");
Border bevelraised=BorderFactory.createBevelBorder(BevelBorder.RAISED);
button.setBorder(bevelraised);
*/

Note:-Remove Comments from all the code before use.

Read more...

Tuesday, July 7, 2009

How to delete a record in database through JDBC

There is a straight forward syntax which can be used to delete the rows in a record set or database.In JDBC you can use executeUpdate(String query) here argument query indicates the complete query for the deletion of records.

stmt.executeUpdate("delete employee where empid=0000");
//stmt is the Reference variable for PreparedStatement

executeUpdate() returns the number of queries affected.

In case if you want to delete the record at runtime say in employee table which is used in above example.You may wish to give empid at runtime.Thus in that case you have to use placeholders.

That is previous example may now be

stmt.executeUpdate("delete employee where empid=?");

but here in this case you have to provide the value for placeholder by using setString() or setInt() method according to your need.

Code Snippet:-
Following example uses placeholders

public void deleterecord(){
int val=1002;
try{
stmt.setInt(1,val);
stmt.executeUpdate("delete employee where empid=?");
}catch(Exception e){}
}

For More on JDBC goto:-

JDBC:PreparedStatement Object
JDBC:Statement Object
JDBC:ResultSet Object
querying database
connecting to database
Configuring data source
JDBC :Introduction


Read more...

Saturday, July 4, 2009

Why Java not support multiple Inheritance?

One can always think of extending multiple classes to inherit the multiple features in the form of methods.Like following:-

class Car extends Accelerate,Color{ //interesting code}

but the above declaration is not legal in terms if Java.A class cannot extend more than one class.This exactly implies that only one parent class per class.But a class can have multiple ancestors such that if class A extends class B and class B extends class C then class A has two ancestors class B and C .A class can have multiple ancestors up the inheritance tree but it is not possible by direct declaration of extending classes.

In C++ ,One can extend multiple classes simultaneously that is the declaration we have done for class Car is correct in case of C++.Capability of extending multiple classes is called as "Multiple Inheritance".Java creators thought a lot about allowing the multiple inheritance,but they were messed up after using the multiple inheritance.Like in this case ,if a class extended two classes and if both classes has method(s) in common then how the methods will be inherited and how we will come to know that which method is called of which class.That is why they excluded the multiple inheritance in Java.There is a famous problem which is faced during the multiple inheritance application called as "Deadly Diamond of Death".This is named so because the shape of class diagram which is formed after multiple inheritance implementation.The diamond is formed when classes B and C extend A ,and both B and C inherit methods from A.If class D extends both B and C,and both B and C have overridden the methods in class A,class D in theory has inherited the two different implementations of same method.


There is an indirect way by which you can implement the multiple inheritance in Java and this is through extending a class and implementing an interface just like following:-

class Car extends Accelerate implements Movable{
// implement all Movable methods
}

In this way you can get methods from both class and interface and you can override them according to your use.Remember all methods of interface needs to be overridden in implementing class.

Read more...

Thursday, July 2, 2009

Polymorphism:Core of Object Oriented Programming

Java is an Object Oriented Language and Polymorphism is its core concept.Nobody can program efficiently without knowing the core programming techniques like Polymorphism,Code Reuse etc.The genuine meaning of polymorphism is "many forms".In Java this meaning is rightly justified.Polymorphism in Java means a method can have various versions.It may be in A super class and it can also be in sub class.There are ways to invoke the methods from either super class or the sub class.As you are familiar with Java you must know that a sub class in Java inherits all the methods of its super class(except methods are declared private in super class).Every sub class can provide its own functionality to every super class method.That is in simpler terms the method is overridden in sub class.We can invoke the super class method using both reference variable types either as of super class or of sub class.

Following Example make things clear for you.

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
super.imageShape();
System.out.println("Square Shape Printed");
}

public void shape_square(){
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
Shape shp=new Shape();
sqr.imageShape();
}
}

Output:-
The output of above program is
Image Shape Printed
Square Shape Printed

Explanation:-
There are certain you must have noticed in the code.First of all starting from our super class Shape it has one method imageShape().The sub class Square inherits the method imageShape() from its parent Shape.Square class provides square specific functionality to this method.
Note:-super keyword can be used to refer to the super class version of imageShape() method.
never use super outside method body.If you do so compiler don't hesitate to slap you.You can also add your additional methods to your more specific classes as we did here.
TestShape class makes object of the Square class and invokes method imageShape().This reference cause the sub class version of imageShape() which results in the presented output.

The story is not as you like there are some problems for you I know you can solve them,if possible let me know the answers with proper explanations:-


1.What would be the output of following:-

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
System.out.println("Square Shape Printed");
}

public void shape_square(){
imageShape();
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
sqr.imageShape();
sqr.shape_square();
}
}

--------------------------------------------------------------------

2.What would be the output of following code:-

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
super.imageShape();
System.out.println("Square Shape Printed");
}

imageShape();
public void shape_square(){
imageShape();
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
sqr.imageShape();
}
}

Read more...

Wednesday, July 1, 2009

Why code reuse is an exclusive feature of Inheritance??

The Inheritance in Java can have two ways of implementation:-

1.Code Reuse
2.Polymorphism

Since Inheritance is the core concept of OO programming ,thus it is must to understand "Code Reuse".Code Reuse lets a programmer to use the code of more generic super-class into subclasses.You need not write extra code for any of your method if the behaviour is common to both super class and subclass.As we know Super classes are more general whereas the subclasses are more of a special kind,that is why the general methods and variables are defined in super classes.Whereas a subclass can either extend the functionality or use whatever given in super class.

Code Reuse Example:-

class Car{
public void color(){

System.out.println("Color of your car is: Black");
}
}

Public class Subaru extends Car{
public void accelerate(){

System.out.println("Top speed of Subaru is 120 mph");
}
public static void main(String a[]){
Subaru sbr=new Subaru();
sbr.color();
sbr.accelerate();
}
}

Output :-
Color of your car is: Black
Top speed of Subaru is 120 mph

Here you can see that class Subaru extends Car thus it Inherits the color() method of Subaru.Also it can also add its own method as accelerate() which is only Subaru specific that its top speed is 120mph.That is every can have its own color and its generic for every car thus every car extending the Car class need not reimplement the color() method.

Home

Read more...

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP