POLYMORPHISM IN JAVA in Hindi Lecture 64
//Polymorphism in Java Polymorphism means "one thing but many forms".Poly means many and morphism means forms.There are two types of Polymorphism in Java---- 1.Compile Time Polymorphism 2.Run Time Polymorphism -----For Compile Time Polymorphism we use Method Overloading. -----For Run Time Polymorphism we use Method Overriding. Compile Time Poymorphism----Compile-Time Polymorphism in Java is also known as static polymorphism and is achieved mainly through method overloading. Method Overloading in Java allows us to create methods with the same name but with different data types and different number of parameters. Different Approaches of Method Overloading-----Method Overloading in Java can be achieved in the following two ways-- 1.By Changing the Number of Arguments---Method Overloading is achieved by defining methods with the same name but a different number of parameters. 2.By Changing the Data Type---Method overloading can also be achieved by keeping the number of parameters same but changing their data types. Example----- 1.To perform add operation(By Changing Data Type) class student { public int add(int a,int b) { return a+b; } public double add(double a,double b) { return a+b; } } 2.Run Time Polymorphism---Run time Polymorphism in Java is also as dynamic method dispatch.It occurs when a method call is resolved at run time and it is achieved using method overriding. Method Overriding is used to provide the specific implementation of a method that is already provided by its super class. 1.In other words the overriding method in the subclass must have the same name as the method in the superclass then it is called method overriding . 2.The overriding method must have the same number and types of parameters as the method in the superclass. 3.Method Overridding is used for runtime polymorphism.The method call is resolved at run time based on the actual Object type. Example------ class student { //parent class method public void show() { //code } } class person extends student { //child class method(method overridden as same name used in parent class) public void show() { //code } } Use of super() keyword-----The super keyword provides a way for a subclass to access members(methods,variables or constructors) of its immediate parent class. Syntax--- super.variablename; //variable name of parent class super.methodname(); // method name of parent class
Download
0 formatsNo download links available.