Monday, 16 September 2013
Overloading is a popular technique of object oriented programming. It allows you to define a method with same name but with different parameters list.
Java identifies method by its name and signature. So, if the combination of method name and signature is unique, the compiler doesn’t have any issue to identify it. When you use overloaded methods, the java compile will automatically identifies the right version of the method by number and/or types of passing parameters. But you can’t declare more than one method with same name and passing parameters. The compiler doesn’t consider return types when validating uniqueness of a method. That means you can’t have two methods with same name and passing parameters with different return type.
Java allows both method and constructor overloading. Let’s see this with an example.
Overriding is the technique of inheriting a class and redefining the behaviors of the super class. That means, when you define a method in subclass with the same signature (name, plus the number and the type of its parameters) and return type as in the superclass, then you are overriding the method of the superclass in your subclass, and the technique is known as method overriding.
In method overriding, method name, method input parameters and return types in subclass should be same in superclass. It can only modify the behaviors. It can return a subtype of the original return type defined in superclass, and this technique is known as covariant return type [Java 1.5 or above support this]. For example, suppose the return type in superclass is java.util.Map, you can change it to java.util.HashMap in subclass, as HashMap is a subtype of Map.
You can also use @Override annotation [which is available in Java 1.5 or above] to tell the compiler that you are trying to override a method in the superclass. And, fof any reason, if the compile is unable to detect that method, it will generate an error.
In method overriding in java, you can level up the access level but you can’t level it down. That means, if a method is declared as protected in superclass, it can be set as public is subclass but not as private.
If a method is declared as final, it can’t be overridden.
Now let’s see with an example.
Monday, 16 September 2013 by anij · 1
Wednesday, 14 August 2013
To deserialize an object from file, first get the serialized data from the file using FileInputStream. In case if you want to know how to serialize an object to file please check this article serialize an object to a file. Pass the FileInputStream object through ObjectInputStream. Now, get the object from ObjectInputStream using the API readObject().
Suppose we have a file named sampleClass.ser which contains the data of a serialized object of class SampleClass. Please check this post to create this file. Where SampleClass is a normal pojo class with 4 variables, among which 1 is declared as transient, that means, protected from serialization.
Wednesday, 14 August 2013 by anij · 2
Sunday, 11 August 2013
When a class contains any abstract method, it must be declared as abstract.
An abstract class cannot be
An abstract method cannot be
Sunday, 11 August 2013 by anij · 1