Polymorphism in java with examples

Views:
 
Category: Education
     
 

Presentation Description

polymorphism in java is well explained in this tutorial with easy to understand examples. we provide also a comparison between java dynamic polymorphism vs java static polymorphism. The online version of this tutorial is here: http://how-to-program-in-java.com/2016/08/17/polymorphism-java-examples-static-dynamic/ you may check our java tutorial for more tutorials: http://how-to-program-in-java.com/core-java-tutorial/

Comments

Presentation Transcript

slide 1:

Polymorphism in java with examples 1 Introduction to polymorphism in java Polymorphism is an important object oriented concept. It is not related to a specific programming language. Each object oriented language provides its own implementation of polymorphism. JAVA as an object oriented language provides its own implementation of polymorphism. In this tutorial we will deal with polymorphism in java. The polymorphism is a compound name. It is composed by two words:  Poly: means many  Morphisms: means forms So polymorphism means many forms. There are two types of polymorphism:  static or compile time polymorphism  dynamic or runtime polymorphism 2 Static polymorphism in java static polymorphism in java is achieved through method overloading. In method overloading a class has many methods having the same name and different list of parameters. The overloaded methods have many forms many signatures .The right form of the method to call is determined based on the parameters passed to that method when calling it. In the context of static polymorphism the binding between the method call and its definition is determined by the compiler during the compile time. We say that we are in the context of static binding. In the static binding the method to call is determined by the type of the object during its declaration. a. Static polymorphism in java example We will provide an example of static polymorphism in java: public class Artist public void work System.out.println"I am making an artistic work"

slide 2:

public void workString name System.out.println"I am making an artistic work called "+ name The main class that contains the main method is the following: public class TestArtist public static void mainString args Artist artist new Artist artist.work artist.work"Monalisa" The work method in the Artist class has two forms:  The form without parameters  The form with a single parameter of type String The right form of the work method is called during compile time. This is what we call static binding. In fact this is method overloading in java. After the execution of the main method we will get the following output: I am making an artistic work I am making an artistic work called Monalisa 3 dynamic polymorphism in java dynamic polymorphism in java is achieved through method overriding. The method overriding is tightly related to the concept of inheritance. You can check our tutorial about inheritance for more informations about this concept. Let’s consider this use case:  ClassA is a parent class that has a method called method1  ClassB inherits from classA and provides its own implementation of method1  ClassC inherits from classA and provides its own implementation of method1 We say that ClassB and ClassC override the method1 defined in the parent class classA. The relationship between an object of child type and and object of the parent type is IS A:

slide 3:

 An object of type ClassB is an Object of type ClassA  An object of type ClassC is an Object of type ClassA But the reverse is incorrect: an object of type ClassA is not an Object of class ClassB or ClassC. An object in java has many forms:  The form of its parent class  The form of its effective class it may be more than two forms because all the classes in java inherits from the Object class So the following code is legitimate: ClassA object1 new ClassB The same thing can be done with ClassC: ClassA object1 new ClassC We will use ClassB for the rest of the example. The question here is which method will be called after the execution of this line of code: object1.method1 Here we have two possibilities:  The method method1 of the class ClassA is called  The method method1 of the class ClassB is called The java language here uses dynamic binding. This means that the method to call is determined at the runtime. The method related to the effective type of the object is called. In our example the method method1 of the class ClassB is called. Remark: Let’s suppose that the java language will use static binding which is not the case. In this case the method to call is determined during compile time. So the method to call is determined based on the type of the declared variable ClassA. In this case static binding the method method1 of the class ClassA will be called. I hope that I am not confusing you. But I want to explain what will be done if java uses static binding when calling overridden methods. a. dynamic polymorphism in java example 1 This is the Artist class which is the parent class:

slide 4:

public class Artist public void workString name System.out.println"I am making an artistic work called "+ name This is the Painter class that inherits from the Artist class: public class Painter extends Artist public void workString name System.out.println"I am creating a new board called "+ name This is is the Singer class that inherits from the Artist class: public class Singer extends Artist public void workString name System.out.println"I am singing a new song called "+ name The main class is the following: public class TestArtists public static void mainString args Artist artist new Painter artist.work"Monalisa" The ouput obtained after executing the main class is the following: I am creating a new board called Monalisa The method work of the Painter class is executed. The dynamic binding dictates that the method of the effective type Painter gets called. The dynamic binding occurs during runtime. b. Dynamic polymorphism in java example 2 In this example we will use an array of objects to see the dynamic binding in action. The Artist class is the following: public class Artist

slide 5:

protected String creativeWork public Artist public ArtistString work this.creativeWork work public void work System.out.println"I am making an artistic work called "+ creativeWork The Painter class is the following: public class Painter extends Artist public PainterString p this.creativeWork p public void workString name System.out.println"I am creating a new paint called "+ creativeWork The Singer class is the following: public class Singer extends Artist public SingerString song this.creativeWork song public void work System.out.println"I am singing a new song called "+ creativeWork The main class is the following: import java.util.ArrayList import java.util.List public class TestArtist public static void mainString args Painter p1 new Painter"My paint" Singer s1 new Singer"My song" ListArtist artists new ArrayListArtist artists.addp1 artists.adds1 forint i 0 i artists.size i++

slide 6:

artists.geti.work polymorphism in java : conclusion This java polymorphism tutorial arrives at its end. We have explained dynamic and static polymorphism with many examples. If this java tutorial was helpful for you please check our core java tutorial. You can like our how to program facebook page for more videos or follow our how to program Google plus page.

authorStream Live Help