Ad hoc полиморфизм java

Полиморфизм и его виды

Закрываю тему ООП самым сложным принципом. Если с абстракцией, инкапсуляцией и наследованием все относительно просто, то в силу развития самого ООП, о чем поведал в предыдущей заметке , есть подводные камни, как минимум из-за того, что он имеет несколько типов, о чем чуть дальше. Кроме того, на собеседованиях часто называют ошибочно ООП-полиморфизмом всего лишь один вид полиморфизма. Начать разбирать вопрос стоит с общего определения полиморфизма и мне нравится трактовка Сергея Немчинского :

Коротко и ясно. А теперь подробнее о более глубокой стороне вопроса и непосредственно видах полиморфизма.

1. Ad-hoc-полиморфизм

Ad-hoc-полиморфизм — вид полиморфизма при котором в зависимости от типа данных применяются различные функции. В строго типизированных языках инструментом реализации такого полиморфизма является перегрузка методов. В слабо типизированных языках посредством привидения типов.

Ad-hoc полиморфизм на примере Java(перегрузка методов):

public class Program < static int sum(int x, int y)< return x + y; >static double sum(double x, double y) < return x + y; >static int sum(int x, int y, int z) < return x + y + z; >>

Ad-hoc полиморфизм на примере JavaScript (приведение типов):

alert( String(null) === "null" ); // true alert( true + "test" ); // "truetest" alert( "123" + undefined ); // "123undefined"

2. Параметрический полиморфизм

Параметрический полиморфизм — вид полиморфизма, когда функция выполняется вне зависимости от типа аргумента. Такой вид полиморфизма в языках часто раскрывается через Generic’и. Ярким примером является вызов статической функции sort() класса Collections в Java:

public static > void sort(List list)

3. Полиморфизм подтипов

Полиморфизм подтипов — свойство системы, позволяющее использовать объекты с одинаковым интерфейсом без информации о типе и внутренней структуре объекта. На мой взгляд в данном определении слово интерфейс фигурирует не в значении интерфейсов из языков программирования, где определяются функции, которые мы должны реализовать, а в значении контракта. А уже под контрактом у нас может иметься ввиду — как интерфейс, что мы знаем по языкам программирования, так и абстрактный класс.

Читайте также:  Import java sql statement

Источник

Java Polymorphism

Ability of an organism to take different shapes is polymorphism in bio world. A simplest definition in computer terms would be, handling different data types using the same interface. In this tutorial, we will learn about what is polymorphism in computer science and how polymorphism can be used in Java.

I wish this tutorial will help address the following,

  • is overloading polymorphism?
  • is overriding polymorphism?
  • Ad hoc Polymorphism
  • Parametric Polymorphism
  • Coercion Polymorphism
  • Inclusion or subtype Polymorphism
  • what is static-binding?
  • what is dynamic binding?

Types of Polymorphism

Polymorphism in computer science was introduced in 1967 by Christopher Strachey. Please let me know with reference if it is not a fact and the tutorial can be updated. Following are the two major types of polymorphism as defined by Strachey.

Later these were further categorized as below:

Polymorphism

Ad hoc Polymorphism

«Ad-hoc polymorphism is obtained when a function works, or appears to work, on several different types (which may not exhibit a common structure) and may behave in unrelated ways for each type. Parametric polymorphism is obtained when a function works uniformly on a range of types; these types normally exhibit some common structure.» – Strachey 1967

If we want to say the above paragraph in two words, they are operator overloading and function overloading. Determining the operation of a function based on the arguments passed.

Ad hoc Polymorphism in Java

In Java we have function overloading and we do not have operator overloading. Yes we have “+” operator implemented in a polymorphic way.

String fruits = "Apple" + "Orange"; int a = b + c;

The definition is when the type is different, the internal function adjusts itself accordingly. int and float are different types and so even the following can be included in polymorphism operator overloading.

int i = 10 - 3; float f = 10.5 - 3.5;

Similarly even * and / can be considered as overloaded for int and float types.

Having said all the above, these are all language implemented features. Developers cannot custom overload an operator. So answer for the question, “does Java supports operator overloading?” is “yes and no”.

Java wholeheartedly supports function overloading. We can have same function name with different argument type list. For function overloading in Java I have already written a super-hit tutorial and I am sure you will enjoy reading it.

In inheritance, the ability to replace an inherited method in the subclass by providing a different implementation is overriding. Function overriding in is discussed in the same tutorial as overloading.

Polymorphism is a larger concept which consists of all these different types. So it is not right to say that overloading or overriding alone is polymorphism. It is more than that.

Coercion Polymorphism

Implicit type conversion is called coercion polymorphism. Assume that we have a function with argument int. If we call that function by passing a float value and if the the run-time is able to convert the type and use it accordingly then it is coercion polymorphism.

Now with this definition, let us see if Java has coercion polymorphism. The answer is half yes. Java supports widening type conversion and not narrowing conversions.

Narrowing Conversion

class FToC < public static float fToC (int fahrenheit) < return (fahrenheit - 32)*5/9; >public static void main(String args[]) < System.out.println(fToC(98.4)); >>

Java does not support narrowing conversion and we will get error as «FToC.java:7: fToC(int) in FToC cannot be applied to (double)»

Widening Conversion

class FToC < public static float fToC (float fahrenheit) < return (fahrenheit - 32)*5/9; >public static void main(String args[]) < System.out.println(fToC(98)); >>

The above code will work without an error in Java. We are passing an int value ’98’ wherein the expected value type is a float. Java implicitly converts int value to float and it supports widening conversion.

Universal Polymorphism

Universal polymorphism is the ability to handle types universally. There will be a common template structure available for operations definition irrespective of the types. Universal polymorphism is categorized into inclusion polymorphism and parametric polymorphism.

Inclusion polymorphism (subtype polymorphism)

Substitutability was introduced by eminent Barbara Liskov and Jeannette Wing. It is also called as Liskov substitution principle.

“Let T be a super type and S be its subtype (parent and child class). Then, instances (objects) of T can be substituted with instances of S.”

Replacing the supertype’s instance with a subtype’s instance. This is called inclusion polymorphism or subtype polymorphism. This is covariant type and the reverse of it is contravariant. We have discussed the substitution principle and covariant types, contravariant and invariant earlier in the linked tutorial. This is demonstrated with a code example. Java supports subtype polymorphism from Java / JDK version 1.5.

Parametric Polymorphism

Here we go, we have come to ‘Generics’. This is a nice topic and requires a full detailed tutorial with respect to Java. For now, parametric polymorphism is the ability to define functions and types in a generic way so that it works based on the parameter passed at runtime. All this is done without compromising type-safety.

The following source code demonstrates a generics feature of Java. It gives the ability to define a class and parameterize the type involved. The behavior of the class is based on the parameter type passed when it is instantiated.

package com.javapapers.java; import java.util.ArrayList; import java.util.List; public class PapersJar  < private ListitemList = new ArrayList(); public void add(T item) < itemList.add(item); >public T get(int index) < return itemList.get(index); >public static void main(String args[]) < PapersJarpapersStr = new PapersJar(); papersStr.add("Lion"); String str = papersStr.get(0); System.out.println(str); PapersJar papersInt = new PapersJar(); papersInt.add(new Integer(100)); Integer integerObj = papersInt.get(0); System.out.println(integerObj); > >

Static Binding vs Dynamic Binding

Give all the above polymorphism types, we can classify these under different two broad groups static binding and dynamic binding. It is based on when the binding is done with the corresponding values. If the references are resolved at compile time, then it is static binding and if the references are resolved at runtime then it is dynamic binding. Static binding and dynamic binding also called as early binding and late binding. Sometimes they are also referred as static polymorphism and dynamic polymorphism.

Let us take overloading and overriding for example to understand static and dynamic binding. In the below code, first call is dynamic binding. Whether to call the obey method of DomesticAnimal or Animal is resolve at runtime and so it is dynamic binding. In the second call, whether the method obey() or obey(String i) should be called is decided at compile time and so this is static binding.

package com.javapapers.java; public class Binding < public static void main(String args[]) < Animal animal = new DomesticAnimal(); System.out.println(animal.obey()); DomesticAnimal domesticAnimal = new DomesticAnimal(); System.out.println(domesticAnimal.obey("Ok!")); >> class Animal < public String obey() < return "No!"; >> class DomesticAnimal extends Animal < public String obey() < return "Yes!"; >public String obey(String i) < return i; >>

Advantages of Polymorphism

  • Generics: Enables generic programming.
  • Extensibility: Extending an already existing system is made simple.
  • De-clutters the object interface and simplifies the class blueprint.

Источник

Java Pylymorphism

The polymorphism is the process of defining same method with different implementation. That means creating multiple methods with different behaviors.

In java, polymorphism implemented using method overloading and method overriding.

Ad hoc polymorphism

The ad hoc polymorphism is a technique used to define the same method with different implementations and different arguments. In a java programming language, ad hoc polymorphism carried out with a method overloading concept.

In ad hoc polymorphism the method binding happens at the time of compilation. Ad hoc polymorphism is also known as compile-time polymorphism. Every function call binded with the respective overloaded method based on the arguments.

The ad hoc polymorphism implemented within the class only.

Let’s look at the following example java code.

import java.util.Arrays; public class AdHocPolymorphismExample < void sorting(int[] list) < Arrays.parallelSort(list); System.out.println("Integers after sort: " + Arrays.toString(list) ); >void sorting(String[] names) < Arrays.parallelSort(names); System.out.println("Names after sort: " + Arrays.toString(names) ); >public static void main(String[] args) < AdHocPolymorphismExample obj = new AdHocPolymorphismExample(); int list[] = ; obj.sorting(list); // Calling with integer array String[] names = ; obj.sorting(names); // Calling with String array > > 

When we run this code, it produce the following output.

polymorphism in java

Pure polymorphism

The pure polymorphism is a technique used to define the same method with the same arguments but different implementations. In a java programming language, pure polymorphism carried out with a method overriding concept.

In pure polymorphism, the method binding happens at run time. Pure polymorphism is also known as run-time polymorphism. Every function call binding with the respective overridden method based on the object reference.

When a child class has a definition for a member function of the parent class, the parent class function is said to be overridden.

The pure polymorphism implemented in the inheritance concept only.

Let’s look at the following example java code.

Источник

Оцените статью