1 / 23

Polymorphism in Java

Polymorphism is the ability of an object to take more than one form. It is one of the important concepts of object-oriented programming language.

Télécharger la présentation

Polymorphism in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Copyright @ 2015 Learntek. All Rights Reserved.

  2. Polymorphism in Java

  3. What is Polymorphism? Polymorphism is the ability of an object to take more than one form. It is one of the important concepts of object-oriented programming language. JAVA is an object-oriented programming language that supports the concept of polymorphisms. Alternatively, it is defined as the ability of a reference variable to change behavior according to what object instance it is holding. This allows multiple objects of different subclasses to be treated as objects of a single parent class, while automatically selecting the proper methods to apply to an object based on the child class it belongs. Copyright @ 2018 Learntek. All Rights Reserved.

  4. There are two types of polymorphism in Java: 1) Compile-time polymorphism (static binding)2) Runtime polymorphism (dynamic binding) Method overloading is an example of compile-time polymorphism, while method overriding is an example of runtime polymorphism. Copyright @ 2018 Learntek. All Rights Reserved.

  5. 1. Compile-time Polymorphism: The type of polymorphism that is implemented when the compiler compiles a program is called compile-time polymorphism. This type of polymorphism is also called as static polymorphism or early binding. Copyright @ 2018 Learntek. All Rights Reserved.

  6. Method Overloading: If a class has multiple methods having the same name but different in parameters, it is known as Method Overloading. Method overloading is performed within the class. If we must perform only one operation, having the same name as the methods increase the readability of the program. There are three ways to overload the method in java• By changing the number of arguments• By changing the data type• By changing both numbers of arguments and data type Copyright @ 2018 Learntek. All Rights Reserved.

  7. a. Method overloading by changing the number of argument: Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as x(int, int) for two parameters, and y(int, int, int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs Polymorphism in java— Example1:In the following program, two methods are created, first add1() performs addition of two numbers and second add1() performs addition of three numbers. Copyright @ 2018 Learntek. All Rights Reserved.

  8. In following example, we are creating static methods so that we don’t need to create instance for calling methods. class Addition{static int add1 (int a,int b){return a+b;}static int add1(int a,intb,int c){return a+b+c;}} Copyright @ 2018 Learntek. All Rights Reserved.

  9. class xyz{ public static void main(String[] args){System.out.println(Addition.add1(13,13));System.out.println(Addition.add1(9,9,9));}}Output:2627 Copyright @ 2018 Learntek. All Rights Reserved.

  10. b. Method Overloading by changing the data type: Sometime there is need to use the same method which is having different data type. In the following program, we have created two methods that differs in data type. The first add1() method receives two integer arguments and second add1() method receives two double arguments. class Addition{static int add1 (int a,int b){return a+b;} Copyright @ 2018 Learntek. All Rights Reserved.

  11. static double add1(double a,double b){return a+b;}}class xyz1{public static void main(String[] args){System.out.println(Addition.add1(13,13));System.out.println(Addition.add1(9.5,9.5));}} Copyright @ 2018 Learntek. All Rights Reserved.

  12. Output:2619 c. Method Overloading by changing both number of argument and data type: Sometimes we may need to use the method which is having the different number of argument and different data type. In the following program, both number of argument and data type is to be changed. Here the method do() is overloaded 3 times: first having one int parameter, second one has 2 int parameters and third one is having double arg. The methods are invoked or called with the same type and number of parameters used. Copyright @ 2018 Learntek. All Rights Reserved.

  13. class Overload1{void do (int a){System.out.println (“a: ” + a);}void do (int a, int b){System.out.println (“a and b: ” + a + “,” + b);}double do(double a){System.out.println(“double a: ” + a);return a*a;}} Copyright @ 2018 Learntek. All Rights Reserved.

  14. class Overload2{public static void main (String args []){Overload1 Obj = new Overload1();double result;Obj.do(20);Obj.do(20, 30);result = Obj.do(4.5);System.out.println(“Output is: ” + result);}}a: 20a and b: 20,30double a: 4.5Output is: 20.25 Copyright @ 2018 Learntek. All Rights Reserved.

  15. 2. Run-time Polymorphism : The type of polymorphism which is implemented dynamically when a program being executed is called as run-time polymorphism. The run-time polymorphism is also called dynamic polymorphism or late binding. Method Overriding in Java Method overriding is used to achieve runtime polymorphism or dynamic binding. In method overriding the method must have the same name as in the parent class and it must have the same parameter as in the parent class. If the subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. Method overriding occurs in two classes that have IS-A (inheritance) relationship. Copyright @ 2018 Learntek. All Rights Reserved.

  16. Example 1 of method overriding In the following program, we have defined the do() method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method are the same, and there is IS-A relationship between the classes, so there is method overriding. //parent classclass Animal{//define methodvoid do(){System.out.println(“Animal is eating food”);}} Copyright @ 2018 Learntek. All Rights Reserved.

  17. //Create child classclass dog extends Animal{//define the same method as in the parent classvoid do(){System.out.println(“Dog is eating food “);}public static void main(String args[]){dog obj = new dog();//creating objectobj.do();//calling method}}Output:Dog is eating food Copyright @ 2018 Learntek. All Rights Reserved.

  18. Example 2 of Method Overriding An example or program is given below from the real-world where Vehicle is parent class. It can have Vehicle class and its specialized child classes like bike and car. These subclasses will override the default behavior provided by Vehicle class and some of its own specific behavior. public class Vehicle{public void run(){System.out.println(“some speed”);}}class bike extends Vehicle{ Copyright @ 2018 Learntek. All Rights Reserved.

  19. public void run(){System.out.println(“speed is slower than car”);}}class car extends Vehicle{public void run(){System.out.println(“speed is faster than bike”);}} // Next, run() method will be called, depends on the type of actual instance  Copyright @ 2018 Learntek. All Rights Reserved.

  20. created on runtime public class Demo{public static void main(String[] args){Vehicle v1 = new bike();v1.run();Vehicle v2 = new car();v2.run();}}Output:speed is faster than a bikespeed is slower than car Copyright @ 2018 Learntek. All Rights Reserved.

  21. Advantage of polymorphism: • It helps programmers to reuse the code, classes, methods written once, tested and implemented. They may be reused in many ways. • The single variable name can be used to store variables of multiple data types such as Int, Float, double, Long, etc). • Polymorphism helps in reducing the coupling between different functionalities. • Method overloading can be implemented on constructors allowing different ways to initialize objects of a class. This enables you to define multiple constructors for handling different types of initializations. • Method overriding works together with inheritance to enable the code-reuse of existing classes without the need for re-compilation. Copyright @ 2018 Learntek. All Rights Reserved.

  22. The disadvantage of polymorphism: • One of the main disadvantages of polymorphism is that developers find it difficult to implement polymorphism in codes. • Run time polymorphism can lead to the performance issue where the machine needs to decide which method or variable to invoke so it basically degrades the performances as decisions are taken at run time. • Polymorphism reduces the readability of the program. One needs to identify the runtime behavior of the program to identify actual execution time. Copyright @ 2018 Learntek. All Rights Reserved.

  23. For more Training Information , Contact Us Email : info@learntek.org USA : +1734 418 2465 INDIA : +40 4018 1306 +7799713624 Copyright @ 2018 Learntek. All Rights Reserved.

More Related