- First difference is, in case of method overloading in Java, Signature of method changes while in case of method overriding it remains same.
- Second difference is that you can overload method in one class but overriding can only be done on subclass.
- You can not override static, final and private method in Java but you can overload static, final or private method in Java.
- Overloaded method in Java is bonded by static binding and overridden methods are subject to dynamic binding.
- Private and final method can also be not overridden in Java.
Notes:
- The final keyword is mostly used to guarantee a good usage of the code. For instance (non-static) methods, this allows the compiler to expand the method (similar to an inline function) if the method is small enough. Sometimes it is required to use it. For instance, a nested class can only access to the members of the top-level class if they are final.
- Java does not support multiple inheritance ie derived class(subclass) cannot have two or more base classes.
- Java has only public inheritance.
- Overloading is achieved using inheritance and overloading is achieved using polymorphism.
- Dynamic data types governs the method selection.
a.mtd();
clsA is the base class and clsB is subclass, here object of clsB is created which is referenced by a1, so when we call mtd() then mth() of clsB will be called not of clsA.
No comments:
Post a Comment