12/24 TIBCO RV FIX PROTOCOL JAVA TUTORIAL

     
    TIBCO RV FIX PROTOCOL JAVA TUTORIAL    
   
Method Overloading vs Method Overriding Java Example
December 24, 2011 at 10:05 AM
 



What is Method Overloading and Method Overriding in Java

Method overloading and method overriding in Java is an important concept to understand and even more important is difference between overloading and overriding in Java. Due to polymorphism in Java you can have two methods with same name, but since two duplicate methods can not reside in a class in Java you either overload a method or override a method. When you overload a method in Java its method signature got changed while in case of overriding method signature remains same but a method can only be overridden in sub class. Since Java supports polymorphism and resolve object at run-time it is capable to call overridden method in Java.

How to overload a method in Java

If you have two methods with same name in one Java class with different signature than its called overloaded method in Java. Generally overloaded method in Java has different set of arguments to perform something based on different number of input. You can also overload constructor in Java, which we will see in below example of method overloading. Binding of overloading method occurs during compile time. To overload a java method just changes its signature. Just remember in order to change signature you either need to change number of argument, or type or argument in Java. Since return type is not part of method signature simply changing return type will result in duplicate method and you will get compilation error in Java. In our example of Loan and PersonalLoan class, createLoan method is overloaded. Since you have two crateLoan() method with one takes one argument lender while other take two argument both lender and interestRate.

How to override a method in Java

Overridden method in Java also shares same name as original method in Java but can only be overridden in sub class. Original method has to be defined inside interface or base class. When you override a method in Java its signature remains exactly same including return type. JVM resolves correct overridden method to call based upon object at run-time. For example in our case when we call personalLoan.toString() method even though personalLoan object is of type Loan actual method called would be from PersonalLoan class because object referenced by personalLoan variable is of type PersonalLoan(). This is very useful technique to modify behavior of a function in Java based on different implementation. Equals in Java , hashcode and compareTo methods are classic example of overridden methods in Java.

Another important point is that you can not override static method in Java because they are associated with Class rather than object and resolved and bonded during compile time and that's the reason you cannot override main method in Java. Similar to static, private and final methods are also not overridden in Java.


Difference between method overloading and overriding in Java

Overloading vs Overriding in Java is one of the popular java interview questions at many companies and at different levels of programmers. Here are some of the silent difference between overloading and overriding in Java. Though more important is to understand how to use both overloading and overriding, these difference are good from interview perspective and gives some basic idea as well:

1) In Case of method overloading in Java, Signature of method changes while in case of overriding it remain same.
2) You can overload method in one class but overriding can only be done on subclass.
3) Static method can not be overridden as they are not associated with object.
4) Overloaded method is bonded by static binding and overridden methods are subject to dynamic binding.
5) Private and final method can also be not overridden in Java.


Example of method overloading and overriding in Java

Here is an example of both method overloading and method overriding. In order to explain the concept we have create two classes Loan and PersonalLoan. createLoan() method is overloaded as it has different version with different signature, while toString() method which is original declared in Object class is overridden in both Loan and PersonalLoan class.

public static void main(String[] args) {

//Example of method overloading in Java
Loan cheapLoan = Loan.createLoan("HSBC");
Loan veryCheapLoan = Loan.createLoan("Citibank");

//Example of method overriding in Java
Loan personalLoan = new PersonalLoan();
personalLoan.toString();
}


}

class Loan{
private double interestRate;
private String customer;
private String lender;

public static Loan createLoan(String lender){
Loan loan = new Loan();
loan.lender = lender;
return loan;
}

public static Loan createLoan(String lender, double interestRate){
Loan loan = new Loan();
loan.lender = lender;
loan.interestRate = interestRate;
return loan;
}

@Override
public String toString() {
return "This is Loan by Citibank";
}


}

class PersonalLoan extends Loan{

@Override
public String toString() {
return "This is PersonalLoan by Citibank";
}


Summary
1) In case of method overloading method signature gets changed while in case of overriding signature remains same.

2) Return type is not part of method signature in Java.

3) Overloaded method can be subject to compile time binding but overridden method can only be bind at run-time.

4) Both overloaded and overridden method has same name in Java.

5) Static method can not be overridden in Java.

6) Since private method is also not visible outside of class, it can not be overridden and method binding happens during compile time.

7) From Java5 onwards you can use annotation in Java to declare overridden method just like we did with @override. @override annotation allows compiler, IDE like NetBeans and Eclipse to cross verify or check if this method is really overrides super class method or not.

Java Tutorials you may like

Media Files
java_logo_50_50.jpg (JPEG Image)
   
   
Method Overloading vs Method Overriding in Java with Example
December 24, 2011 at 10:05 AM
 



Method overloading and method overriding in Java is an important concept to understand and even more important is difference between overloading and overriding in Java. Due to polymorphism in Java you can have two methods with same name, but since two duplicate methods can not reside in a class in Java you either overload a method or override a method. When you overload a method in Java its method signature got changed while in case of overriding method signature remains same but a method can only be overridden in sub class. Since Java supports polymorphism and resolve object at run-time it is capable to call overridden method in Java.


What are Method Overloading and Method Overriding in Java?

In this Java tutorial we will see how Java allows you to create two methods of same name by using method overloading and method overriding. We will also touch based on how methods are bonded or called by Compiler and Java Virtual Machine and finally we will answer of popular interview questions difference between method overloading and method overriding in Java. This article is in my series of Java article which discusses about Interview e.g. Difference between Synchronized Collection and Concurrent Collection or How to Stop Thread in Java. Please let me know if you have some other interview questions and you are looking answer or reason for that and here in javarevisited we will try to find and discuss those interview questions.

How to overload a method in Java

If you have two methods with same name in one Java class with different signature than its called overloaded method in Java. Generally overloaded method in Java has different set of arguments to perform something based on different number of input. You can also overload constructor in Java, which we will see in below example of method overloading. Binding of overloading method occurs during compile time. To overload a java method just changes its signature. Just remember in order to change signature you either need to change number of argument, or type or argument in Java. Since return type is not part of method signature simply changing return type will result in duplicate method and you will get compilation error in Java. In our example of Loan and PersonalLoan class, createLoan method is overloaded. Since you have two crateLoan() method with one takes one argument lender while other take two argument both lender and interestRate.

How to override a method in Java

Overridden method in Java also shares same name as original method in Java but can only be overridden in sub class. Original method has to be defined inside interface or base class. When you override a method in Java its signature remains exactly same including return type. JVM resolves correct overridden method to call based upon object at run-time. For example in our case when we call personalLoan.toString() method even though personalLoan object is of type Loan actual method called would be from PersonalLoan class because object referenced by personalLoan variable is of type PersonalLoan(). This is very useful technique to modify behavior of a function in Java based on different implementation. Equals in Java , hashcode and compareTo methods are classic example of overridden methods in Java.

Another important point is that you can not override static method in Java because they are associated with Class rather than object and resolved and bonded during compile time and that's the reason you cannot override main method in Java. Similar to static, private and final methods are also not overridden in Java.


Difference between method overloading and overriding in Java

Overloading vs Overriding in Java is one of the popular java interview questions at many companies and at different levels of programmers. Here are some of the silent difference between overloading and overriding in Java. Though more important is to understand how to use both overloading and overriding, these difference are good from interview perspective and gives some basic idea as well:

1) In Case of method overloading in Java, Signature of method changes while in case of overriding it remain same.
2) You can overload method in one class but overriding can only be done on subclass.
3) Static method can not be overridden as they are not associated with object.
4) Overloaded method is bonded by static binding and overridden methods are subject to dynamic binding.
5) Private and final method can also be not overridden in Java.


Example of method overloading and overriding in Java

Here is an example of both method overloading and method overriding. In order to explain the concept we have create two classes Loan and PersonalLoan. createLoan() method is overloaded as it has different version with different signature, while toString() method which is original declared in Object class is overridden in both Loan and PersonalLoan class.

public static void main(String[] args) {

//Example of method overloading in Java
Loan cheapLoan = Loan.createLoan("HSBC");
Loan veryCheapLoan = Loan.createLoan("Citibank");

//Example of method overriding in Java
Loan personalLoan = new PersonalLoan();
personalLoan.toString();
}


}

class Loan{
private double interestRate;
private String customer;
private String lender;

public static Loan createLoan(String lender){
Loan loan = new Loan();
loan.lender = lender;
return loan;
}

public static Loan createLoan(String lender, double interestRate){
Loan loan = new Loan();
loan.lender = lender;
loan.interestRate = interestRate;
return loan;
}

@Override
public String toString() {
return "This is Loan by Citibank";
}


}

class PersonalLoan extends Loan{

@Override
public String toString() {
return "This is PersonalLoan by Citibank";
}


Summary
1) In case of method overloading method signature gets changed while in case of overriding signature remains same.

2) Return type is not part of method signature in Java.

3) Overloaded method can be subject to compile time binding but overridden method can only be bind at run-time.

4) Both overloaded and overridden method has same name in Java.

5) Static method can not be overridden in Java.

6) Since private method is also not visible outside of class, it can not be overridden and method binding happens during compile time.

7) From Java5 onwards you can use annotation in Java to declare overridden method just like we did with @override. @override annotation allows compiler, IDE like NetBeans and Eclipse to cross verify or check if this method is really overrides super class method or not.

Java Tutorials you may like

Media Files
java_logo_50_50.jpg (JPEG Image)
   
   
Java.net.BindException: Address already in use: JVM_Bind:8080 Solution
December 20, 2011 at 7:58 PM
 



How to deal with java.net.BindException: Address already in use: JVM_Bind:8080

java.net.BindException: Address already in use: JVM_Bind is a common exception in Java with application trying to connect on a particular port and some other processes either Java or non Java is already connected on that port. You can get "Address already in use: JVM_Bind" error while doing remote debugging in Java in Eclipse, when Eclipse trying to connect to remote Java application, when you are starting tomcat and another instance of tomcat is listening on port 8080 you will get java.net.BindException: Address already in use: JVM_Bind:8080.

Address already use: JVM Bind ExceptionIn this post we will analyze java.net.BindException and trying to figure out cause of "Address already in use: JVM_Bind" before fixing it. This article is in continuation of my earlier tutorial, How to Solve OutOfMemoryError in Java and How to fix ClassNotFoundException in Java.


Address already in use: JVM_Bind:8080

This exception is self explanatory, its saying that a Java application is trying to connect on port 8080 but that port is already used by some other process and JVM Bind to that particular port, here its 8080, is failed. Now to fix this error you need to find out which process is listening of port 8080, we will how to find a process which is listening on a particular port in both windows and Linux.

Find process which is listening on port 8080 in Windows netstat command is your friend, just use netstat with find command  as shown in below example:

C:\>netstat -ano | find "8080"
TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       26732

Last column is PID of process which is listening on port "8080", possibly a tomcat web server. You can verify it by looking into task manager and displaying PID as column.

Find process which is listening on port 8080 in Linux
Great thing is that you can use netstat command in Linux and UNIX as well, though with little difference in option it can show you process listening on a particular port, instead of "-o" I normally use "-p" and then use UNIX grep command to select particular process with PID.

trader@asia:~ netstat -nap | grep 8080

How to solve "java.net.BindException: Address already in use"

Now since you have find out offending process you can kill that process and restart yours if killing that process is OK, otherwise change the port your web server is using and you will not get "java.net.BindException: Address already in use" Exception, but if you can not kill that process than you need to change your web-server configuration or eclipse configuration to listen on different port. In case of tomcat you can change it on connector section of server.xml and in case of eclipse you can see here setting up Eclipse for Java remote debugging.

Common Scenario when you see "Address already in use: JVM_Bind"
1. While doing Java remote debugging in Eclipse and when Eclipse tries to connect your remote java application on a particular port and that port is not free.

2. Starting tomcat when earlier instance of tomcat is already running and bonded to 8080 port. It will fail with SEVERE: Error initializing endpoint java.net.BindException: Address already in use: JVM_Bind:8080
3. "Address already in use jvm_bind" could also comes up with other web and application servers like weblogic, glassfish and webshere.

I don't remember count how many times I have got Address already in use: JVM_Bind ERROR but most of the time it turns out that another instance of same process is running and listening on same port,So watch for it and it can save time for you. Some time this is also called "port already in use JVM_Bind" so don't confuse Address and port is used interchangeably in different places.

Related Java Tutorials:
Media Files
17.jpg (JPEG Image)
   
     
 
This email was sent to raaz.love.2009.auto@blogger.com.
Delivered by Feed My Inbox
PO Box 682532 Franklin, TN 37068
Account Login
Unsubscribe Here Feed My Inbox