POLYMORPHISM It means to process the objects differently based on its data type.The two types are: Static Polymorphism Dynamic Polymorphism The method Overloading comes under Static Polymorphism and Overriding comes under Dynamic Polymorphism Overloading is two methods having the same ame in a same class Overriding means a derived class implementing the super class method.
OVERLOADING PROGRAM class OverloadDemo void test) System.out.println( No parameters"); void test(int a) System.out.println("a:"+a); void test(int a,int b) System.out.Printin ("a and b"ta+ +b);
class Overload public static void main(String args|]) (); OverloadDemo obi-new OverloadDemo ob.test); ob.test(I0 ob.test(10); ob.test(10,20):
OVERRIDE PROGRAM class Vehcile void run0 System.out.println("Vehcile is running); class Bike extends Vehcile void run) void run0) System.out.println("Bike is running safely"); public static void main(String argsI) Bike obj=new obj(); obj.run0:
Trouble shooters . Can we overide a static method? No, a static method cannot be overriden as static method is bound to class whereas instance method is bound to object. Can we Override a main method? No,because it is a static method.
Differences between Overloading and Overriding Overloading is used . to increase the readability of a program Overloading is performed within the Overriding occurs in class only . Overriding is used to provide specific implementation of the method which is there in super class. two classes. e Here,the parameter Here,the parameters must be different in the methods. in the methods must be same
Hey everyone,I am Kshethra. I really love explaining to someone it somehow turned up to my passion.I'm really happy to fulfill my passion .