Object Oriented Programming Andi Nurkholis, S.Kom., M.Kom. Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 April 23, 2020
7.2 Polymorphism 2
Inheritance in Java Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways. 3
4 Inheritance in Java (cont.) For example, think of a superclass called Animal that has a method called Sound(). Subclasses of Animals could be Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the cat meow and the dog bark, etc.):
Example 5 class Animal { public void animalSound() { System.out.println("The animal makes a sound"); } } Now we can create Animal as Super Class and make the animalSound() method:
Example (cont.) Then we can create Cat as Sub Class of Animal and make a new different animalSound() method: 6 class Cat extends Animal { public void animalSound() { System.out.println("The cat says: meow meow"); } }
Example (cont.) 7 class Dog extends Animal { public void animalSound() { System.out.println("The dog says: bark bark"); } } Then we can create Dog as Sub Class of Animal and make a new different animalSound() method:
Example (cont.) 8 class MyMainClass { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal d1 = new Cat(); Animal c1 = new Dog(); myAnimal.animalSound(); //same function name d1.animalSound(); //same function name c1.animalSound(); //same function name } } Now we can create Cat and Dog objects and call the animalSound() method on both of them:
Explanation 9 In previous slide, we have three same function names from three different classes that related each other. What the meaning? Polymorphism means same function name (but different signatures) being uses for different types.
Types of Polymorphism In Java polymorphism is mainly divided into two types: • Run-time Polymorphism • Compile-time Polymorphism 10
11 Run-time Polymorphism In Java, run-time polymorphism can be achieved through method overriding. Suppose the same method is created in the superclass and its subclasses. In this case, the method that will be called depends upon the object used to call the method. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
Illustration In previous example (p. 5-8), that is an example of run-time polymorphism. Lets check the illustration: 12
Explanation 13 In the previous illustration, the method makeSound() has different implementations in two different classes. When we run the program, • The expression d1.makeSound() will call the method of Dog class. It is because d1 is an object of the Dog class. • The expression c1.makeSound() will call the method of Cat class. It is because c1 is an object of the cat class.
Compile-time Polymorphism The compile-time polymorphism can be achieved through method overloading and operator overloading in Java. Method Overloading, there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments. For example: 14 void func() { ... } void func(int a) { ... } float func(double a) { ... } float func(int a, float b) { ... }
15 Example class Demo { public void displayPattern(){ for(int i = 0; i < 10; i++) { System.out.print("*"); } } public void displayPattern(char symbol) { for(int i = 0; i < 10; i++) { System.out.print(symbol); } } }
16 Example (cont.) class Main { public static void main(String[] args) { Demo d1 = new Demo(); d1.displayPattern(); System.out.println("n"); d1.displayPattern('#'); } } //output ********** ##########
Explanation In previous example, the displayPattern() method is overloaded. • If we call the method without passing any arguments, a pattern of * is created. • If we call the method by passing a character as an argument, a pattern of that character is created. 17
Overloading Vs Overriding • In the case of method overriding, methods should be inside different classes. Whereas, in the case of method overloading, methods should be inside the same class. • Method overriding is performed at run-time whereas method overloading is performed at compile-time. 18
So, Why “Polymorphism"? Polymorphism allows us to create consistent code. For example, Suppose we need to render a circle and a square. To do so, we can create a Polygon class and inherited two subclasses Circle and Square from it. In this case, it makes sense to create a method having the same name render() in both these subclasses rather than creating methods with different names. In previous method overloading example, we have used the same method name displayPattern() to display two different patterns for consistency. 19
Thank You, Next … Encapsulation Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 Andi Nurkholis, S.Kom., M.Kom. April 23, 2020

Object Oriented Programming - 7.2. Polymorphism

  • 1.
    Object Oriented Programming Andi Nurkholis,S.Kom., M.Kom. Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 April 23, 2020
  • 2.
  • 3.
    Inheritance in Java Polymorphismmeans "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways. 3
  • 4.
    4 Inheritance in Java(cont.) For example, think of a superclass called Animal that has a method called Sound(). Subclasses of Animals could be Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the cat meow and the dog bark, etc.):
  • 5.
    Example 5 class Animal { publicvoid animalSound() { System.out.println("The animal makes a sound"); } } Now we can create Animal as Super Class and make the animalSound() method:
  • 6.
    Example (cont.) Then wecan create Cat as Sub Class of Animal and make a new different animalSound() method: 6 class Cat extends Animal { public void animalSound() { System.out.println("The cat says: meow meow"); } }
  • 7.
    Example (cont.) 7 class Dogextends Animal { public void animalSound() { System.out.println("The dog says: bark bark"); } } Then we can create Dog as Sub Class of Animal and make a new different animalSound() method:
  • 8.
    Example (cont.) 8 class MyMainClass{ public static void main(String[] args) { Animal myAnimal = new Animal(); Animal d1 = new Cat(); Animal c1 = new Dog(); myAnimal.animalSound(); //same function name d1.animalSound(); //same function name c1.animalSound(); //same function name } } Now we can create Cat and Dog objects and call the animalSound() method on both of them:
  • 9.
    Explanation 9 In previous slide,we have three same function names from three different classes that related each other. What the meaning? Polymorphism means same function name (but different signatures) being uses for different types.
  • 10.
    Types of Polymorphism InJava polymorphism is mainly divided into two types: • Run-time Polymorphism • Compile-time Polymorphism 10
  • 11.
    11 Run-time Polymorphism In Java,run-time polymorphism can be achieved through method overriding. Suppose the same method is created in the superclass and its subclasses. In this case, the method that will be called depends upon the object used to call the method. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
  • 12.
    Illustration In previous example(p. 5-8), that is an example of run-time polymorphism. Lets check the illustration: 12
  • 13.
    Explanation 13 In the previousillustration, the method makeSound() has different implementations in two different classes. When we run the program, • The expression d1.makeSound() will call the method of Dog class. It is because d1 is an object of the Dog class. • The expression c1.makeSound() will call the method of Cat class. It is because c1 is an object of the cat class.
  • 14.
    Compile-time Polymorphism The compile-timepolymorphism can be achieved through method overloading and operator overloading in Java. Method Overloading, there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments. For example: 14 void func() { ... } void func(int a) { ... } float func(double a) { ... } float func(int a, float b) { ... }
  • 15.
    15 Example class Demo { publicvoid displayPattern(){ for(int i = 0; i < 10; i++) { System.out.print("*"); } } public void displayPattern(char symbol) { for(int i = 0; i < 10; i++) { System.out.print(symbol); } } }
  • 16.
    16 Example (cont.) class Main{ public static void main(String[] args) { Demo d1 = new Demo(); d1.displayPattern(); System.out.println("n"); d1.displayPattern('#'); } } //output ********** ##########
  • 17.
    Explanation In previous example,the displayPattern() method is overloaded. • If we call the method without passing any arguments, a pattern of * is created. • If we call the method by passing a character as an argument, a pattern of that character is created. 17
  • 18.
    Overloading Vs Overriding •In the case of method overriding, methods should be inside different classes. Whereas, in the case of method overloading, methods should be inside the same class. • Method overriding is performed at run-time whereas method overloading is performed at compile-time. 18
  • 19.
    So, Why “Polymorphism"? Polymorphismallows us to create consistent code. For example, Suppose we need to render a circle and a square. To do so, we can create a Polygon class and inherited two subclasses Circle and Square from it. In this case, it makes sense to create a method having the same name render() in both these subclasses rather than creating methods with different names. In previous method overloading example, we have used the same method name displayPattern() to display two different patterns for consistency. 19
  • 20.
    Thank You, Next… Encapsulation Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 Andi Nurkholis, S.Kom., M.Kom. April 23, 2020