Anda di halaman 1dari 4

OVERLOADING VS OVERRIDING IN JAVA

Overloading and overriding are completely different. Only the notion about interface (function) name is
same. Overloading is the ability to use same interface name but with different arguments. Purpose of
functions might be same but the way they work will differ based on the argument types. Overriding is
applicable in the context of inheritance. When there is a need to change the behavior of a particular
inherited method, it will be overridden in the sub-class.
Overloading itself is not polymorphism and similarly run-time binding itself is not polymorphism. All
these and more are used to exercise the object oriented property polymorphism.
Overloading and Overriding Key Points
1. private: A java private method cannot be overridden because, it is not accessible to an inheriting
class.
2. final: Method overloading a Java final method is possible but overriding a final method is not
allowed.
3. final: Two methods with same parameters list and in one method we have parameters final, in this
case two methods are not different methods and overloading is not possible.
4. static: Method overloading a Java static method is possible but overriding a static method is not
possible.
5. static: Overriding is in the context of objects (instances) and inheritance. A method declared as static
belongs to the whole class (common for all objects). So there is no point of overriding it in sub-class.
6. static: In overloading if two methods are different only by a static modifier then its not possible to
overload. The arguments list must be different.
Overloading
Lets discuss overloading in detail. In websites, I generally see a overloaded truck as example for
overloading. It symbolizes that, adding more attributes, methods to a class and making it look bulkier is
overloading. Please get it right, in fact when we use overloading for the outsiders view the class will look
compact.
I have shown two images, first one is called Harmonium. Its a classical music instrument from India and
its a primitive version of piano. The other is a modern digital keyboard. In our overloading context,
Harmonium is before overloading and keyboard is after overloading.
This is an attempt to closely mimic overloading:
Java Example for Method Overloading
public class OverloadExample {

public static void main(String args[]) {

System.out.println(playMusic("C sharp","D sharp"));


System.out.println(playMusic("C","D flat","E flat"));
}
public static String playMusic(String c, String d) {
return c+d;
}
public static String playMusic(String c, String d, String e){
return c+d+e;
}
}
Java Overloading Puzzle
package.java;
public class NullArgumentOverloading {
public static void main(String[] args) {
NullArgumentOverloading obj = new NullArgumentOverloading();
obj.overLoad(null);
}

private void overLoad(Object o) {


System.out.println("Object o argument method.");
}

private void overLoad(double[] dArray) {


System.out.println("double array argument method.");
}

}
Can you guess the output?

Overriding
When a class is inherited we get the base class methods and attributes based on the respective access
modifiers. A specific inherited methods behavior may not be suitable in the sub-class. So we replace the
inherited methods behavior as required. After overriding, the old behavior is completely obsolete.
Look at this monster truck, the old small four wheels are replaced with huge wheels to suit the need.
This is overriding in real world.
Overriding in Java / JDK
Implicitly every object in Java is extended from Object class. Object has a method named equals. In
String class the default behavior is overridden. The String equals behavior is modified to check if the
character sequence is same as the compared one and returns true.
Why Object is Super Class in Java?
java.lang.Object class is the super base class of all Java classes. Every other Java classes descends from
Object. Should we say the God class? Why is that so? This Java article is to discuss around it.
In Mathematics, an axiom is a starting point of reasoning using which other statement can be logically
derived. The concept of Object class as super class of all classes in Java looks similar to it.
It is not a explicit requirement forced on the developer. If a class is declared without extending another
class then it will implicitly extend Object class. This is taken care of by the JVM. This is not applicable for
the interfaces. A Java interface does not extends the Object.
Following could be the reasons for this design decision,
By having the Object as the super class of all Java classes, without knowing the type we can pass
around objects using the Object declaration.
Before generics was introduced, imagine the state of heterogeneous Java collections. A collection
class like ArrayList allows to store any type of classes. It was made possible only by Object class
hierarchy.
The other reason would be to bring a common blueprint for all classes and have some list of
functions same among them. I am referring to methods like hashCode(), clone(), toString() and
methods for threading which is defined in Object class.
This concept cannot be generalized to all object oriented programming (OOPS) languages. For instance,
in C++ there is no such super class of all classes.
Super class of String, Object, Class

Following program shows


1. the super class of String class.
2. the super class of Object class
3. the super class of Class class
package.java
public class SuperClass {
public static void main(String... args) {
String str = new String("Hi");
Class strClass = str.getClass();
System.out.println("Super class of String: " + strClass.getSuperclass());
Object obj = new Object();
Class objClass = obj.getClass();
System.out.println("Super class of Object: " + objClass.getSuperclass());

Class classClass = objClass.getClass();


System.out.println("Super class of Class: "+ classClass.getSuperclass());
}
}
Output
Super class of String: class java.lang.Object
Super class of Object: null
Super class of Class: class java.lang.Object

Anda mungkin juga menyukai