What cannot be used for polymorphism
Table of contents Show
Polymorphism is the ability of an object to take on different forms. In Java, polymorphism refers to the ability of a class to provide different implementations of a method, depending on the type of object that is passed to the method. To put it simply, polymorphism in Java allows us to perform the same action in many different ways. Any Java object that can pass more than one IS-A test is polymorphic in Java. Therefore, all the Java objects are polymorphic as it has passed the IS-A test for their own type and for the class Object. This article also talks about two types of polymorphism in Java: compile time polymorphism and runtime polymorphism, Java polymorphism examples, method overloading, method overriding, why to use polymorphism in java, java programming, and many more. Polymorphism is a feature of the object-oriented programming language, Java, which implies that you can perform a single task in different ways. In the technical world, polymorphism in Java allows one to do multiple implementations by defining one interface.
What is Polymorphism?The derivation of the word Polymorphism is from two different Greek words- poly and morphs. “Poly” means numerous, and “Morphs” means forms. So, polymorphism means innumerable forms. Polymorphism, therefore, is one of the most significant features of Object-Oriented Programming. What is Polymorphism in Java?Polymorphism in Java is the task that performs a single action in different ways. So, languages that do not support polymorphism are not ‘Object-Oriented Languages’, but, ‘Object-Based Languages’. Ada, for instance, is one such language. Since Java supports polymorphism, it is an Object-Oriented Language. Polymorphism occurs when there is inheritance, i.e. there are many classes that are related to each other. Inheritance is a powerful feature in Java. Java Inheritance lets one class acquire the properties and attributes of another class. Polymorphism in Java allows us to use these inherited properties to perform different tasks. Thus, allowing us to achieve the same action in many different ways. Real-Life Examples of PolymorphismAn individual can have different relationships with different people. A woman can be a mother, a daughter, a sister, a friend, all at the same time, i.e. she performs other behaviours in different situations. The human body has different organs. Every organ has a different function to perform; the heart is responsible for blood flow, lungs for breathing, brain for cognitive activity, and kidneys for excretion. So we have a standard method function that performs differently depending upon the organ of the body. Polymorphism in Java ExampleA superclass named “Shapes” has a method “area()”. Subclasses of “Shapes” can be “Triangle”, “circle”, “Rectangle”, etc. Each subclass has its way of calculating area. Using Inheritance and Polymorphism means, the subclasses can use the “area()” method to find the area’s formula for that shape.
Output: The formula for the area of Triangle is ½ * base *
height Also Read: OOPs concepts in Java Types of PolymorphismYou can perform Polymorphism in Java via two different methods:
What is Method Overloading in Java?Method overloading is the process that can create multiple methods of the same name in the same class, and all the methods work in different ways. Method overloading occurs when there is more than one method of the same name in the class. Example of Method Overloading in Java
Output: Find area What is Method Overriding in Java?Method overriding is the process when the subclass or a child class has the same method as declared in the parent class. Example of Method Overriding in Java
Output: Car is running safely Also, Polymorphism in Java can be classified into two types, i.e:
What is Compile-Time Polymorphism in Java?Compile Time Polymorphism In Java is also known as Static Polymorphism. Furthermore, the call to the method is resolved at compile-time. Compile-Time polymorphism is achieved through Method Overloading. This type of polymorphism can also be achieved through Operator Overloading. However, Java does not support Operator Overloading. Method Overloading is when a class has multiple methods with the same name, but the number, types, and order of parameters and the return type of the methods are different. Java allows the user freedom to use the same name for various functions as long as it can distinguish between them by the type and number of parameters. Check out some of the important questions on run time polymorphism in java interview questions. Example of Compile-Time Polymorphism in JavaWe will do addition in Java and understand the concept of compile time polymorphism using subtract()
The output of the program will be: Sum of two numbers: 120 Sum of three numbers: 147 In this program, the sum() method overloads with two types via different parameters. This is the basic concept of compile-time polymorphism in java where we can perform various operations by using multiple methods having the same name. What is Runtime Polymorphism in Java?Runtime polymorphism in Java is also popularly known as Dynamic Binding or Dynamic Method Dispatch. In this process, the call to an overridden method is resolved dynamically at runtime rather than at compile-time. You can achieve Runtime polymorphism via Method Overriding. Method Overriding is done when a child or a subclass has a method with the same name, parameters, and return type as the parent or the superclass; then that function overrides the function in the superclass. In simpler terms, if the subclass provides its definition to a method already present in the superclass; then that function in the base class is said to be overridden. Also, it should be noted that runtime polymorphism can only be achieved through functions and not data members. Overriding is done by using a reference variable of the superclass. The method to be called is determined based on the object which is being referred to by the reference variable. This is also known as Upcasting. Upcasting takes place when the Parent class’s reference variable refers to the object of the child class. For example:
Examples of Runtime Polymorphism in JavaExample 1: In this example, we are creating one superclass Animal and three subclasses, Herbivores, Carnivores, and Omnivores. Subclasses extend the superclass and override its eat() method. We will call the eat() method by the reference variable of Parent class, i.e. Animal class. As it refers to the base class object and the base class method overrides the superclass method; the base class method is invoked at runtime. As Java Virtual Machine or the JVM and not the compiler determines method invocation, it is, therefore, runtime polymorphism.
Output: Animals eat Example 2: In this example, we are creating one superclass Hillstations and three subclasses Manali, Mussoorie, Gulmarg. Subclasses extend the superclass and override its location() and famousfor() method. We will call the location() and famousfor() method by the Parent class’, i.e. Hillstations class. As it refers to the base class object and the base class method overrides the superclass method; the base class method is invoked at runtime. Also, as Java Virtual Machine or the JVM and not the compiler determines method invocation, it is runtime polymorphism.
Output: Location is: Example of run-time polymorphism in javaWe will create two classes Car and Innova, Innova class will extend the car class and will override its run() method.
The output of the following program will be; Running fast at 120 km. Another example for run-time polymorphism in Java Now, let us check if we can achieve runtime polymorphism via data members.
The output of the following program will be : 125 This clearly implies we can’t achieve Runtime polymorphism via data members. In short, a method is overridden, not the data members. Runtime polymorphism with multilevel inheritance
The output of the following program will be: Swimming, Swimming in river, Swimming in pool Another runtime polymorphism with multilevel inheritance example
The output of the following program will be; The buffalo sound- gho,gho The snake sound- his,his The tiger sound- roooo,roooo We hope you got an idea about runtime and compile-time polymorphism. Polymorphic SubtypesSubtype basically means that a subtype can serve as another type’s subtype, sounds a bit complicated? Let’s understand this with the help of an example: Assuming we have to draw some arbitrary shapes, we can introduce a class named ‘shape’ with a draw() method. By overriding draw() with other subclasses such as circle, square, rectangle, trapezium, etc we will introduce an array of type ‘shape’ whose elements store references will refer to ‘shape’ subclass references. Next time, we will call draw(), all shapes instances draw () method will be called. This Subtype polymorphism generally relies on upcasting and late binding. A casting where you cast up the inheritance hierarchy from subtype to a supertype is termed upcasting. To call non-final instance methods we use late binding. In short, a compiler should not perform any argument checks, type checks, method calls, etc, and leave everything on the runtime. What is Polymorphism in Programming?Polymorphism in programming is defined usage of a single symbol to represent multiple different types. What is Polymorphism Variables?A polymorphic variable is defined as a variable that can hold values of different types during the course of execution. Why use Polymorphism in Java?Polymorphism in Java makes it possible to write a method that can correctly process lots of different types of functionalities that have the same name. We can also gain consistency in our code by using polymorphism. Advantages of Polymorphism in Java
Characteristics of PolymorphismPolymorphism has many other characteristics other than Method Overloading and Method Overriding. They include:
1. CoercionCoercion deals with implicitly converting one type of object into a new object of a different kind. Also, this is done automatically to prevent type errors in the code. Programming languages such as C, java, etc support the conversion of value from one data type to another data type. Data type conversions are of two types, i.e., implicit and explicit. Implicit type conversion is automatically done in the program and this type of conversion is also termed coercion. For example, if an operand is an integer and another one is in float, the compiler implicitly converts the integer into float value to avoid type error. Example:
Output: 109.9
|