Question

In unit 6 we are introduced to the concept of polymorphism which is derived from the...

In unit 6 we are introduced to the concept of polymorphism which is derived from the Greek words poly, which means many and morpheus, which means form, structure. Discuss polymorphism in objective oriented programming and provide a java example that details the implementation of polymorphism in java objects.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Polymorphism is the building bloc of the Object oriented Programming concepts

Polymorphism is a type of overloading. Parent object reference is used to call the method of child object methods.

We will counter many important instances where we can find the polymorphism helpful.

Polymorphism can also be defined as overloading. Overloading means nothing but the compiler will understand the method to be called based on the type and number of arguments of the method.

So, Overloading is a type of static Polymorphism, because it is judged at Compile time.

Overriding is the type of Dynamic Polymorphism, because it is judged at run time.

I will show an example of different types of Polymorphisms stated above.

For Dynamic Polymorphism - Overriding

Animal.java

public class Animal {

   public void bark() {
       System.out.println("Animals will shout !!");
   }
}

Cat.java

public class Cat extends Animal{
  
   @Override
   public void bark() {
       // TODO Auto-generated method stub
       System.out.println("Cat will say Meaww");
   }

}

Dog.java

public class Dog extends Animal{

   @Override
   public void bark() {
       // TODO Auto-generated method stub
       System.out.println("Dog will say Bow Bow");
   }
  
  
  
  

}

AnimalMain.java

public class AnimalMain {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

      
       Animal a = new Animal();
      
       Cat c = new Cat();
      
       Dog d = new Dog();
      
      
       //Here bark() will be overrided.This is dynamic Polymorphism
       // JVM will decide which bark() needs to called during run time
       System.out.println("Calling bark() method from Animal which is parent refference.");
       a.bark();
       System.out.println("Calling bark() method from Cat which is child refference.");
       c.bark();
       System.out.println("Calling bark() method from Dog which is Child refference.");
       d.bark();
      
       a = c ;
      
      
   }

}

Output

Calling bark() method from Animal which is parent refference.
Animals will shout !!
Calling bark() method from Cat which is child refference.
Cat will say Meaww
Calling bark() method from Dog which is Child refference.
Dog will say Bow Bow

For Static Polymorphism - Overriding

StaticPoly.java

public class StaticPoly {
  
   public static void main(String[] args) {
  
      
       //These both are different methods with same name.
       //But this will be interpreted by the compiler during the compile time itself by matching the argument type and number of arguments.
       //This is called static Polymorphism
       System.out.println("Calling printHI() method without argument");
       printHi();
       System.out.println("Calling printHi(5) method with argument");
       printHi(5);
   }

   private static void printHi(int i) {
       // TODO Auto-generated method stub
      
       for(int j=0;j<i;j++)
       System.out.println("Hi");
      
   }

   private static void printHi() {
       // TODO Auto-generated method stub
       System.out.println("Hi");
   }

}

Output

Calling printHI() method without argument
Hi
Calling printHi(5) method with argument
Hi
Hi
Hi
Hi
Hi

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful

Add a comment
Know the answer?
Add Answer to:
In unit 6 we are introduced to the concept of polymorphism which is derived from the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In unit 6 we are introduced to the concept of polymorphism which is derived from the...

    In unit 6 we are introduced to the concept of polymorphism which is derived from the Greek words poly, which means many and morpheus, which means form, structure. Discuss polymorphism in objective oriented programming and provide a java example that details the implementation of polymorphism in java objects.

  • Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to...

    Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to display several different types (classes) of objects. Let's say, we want to display three objects of type Deer, two objects of type Tree and one object of type Hill. Each of them contains a method called display. We would like to store their object references in a single array and then call their method display one by one in a loop. However, in Java,...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • In this assignment you’ll implement a data structure called a trie, which is used to answer...

    In this assignment you’ll implement a data structure called a trie, which is used to answer queries regarding the characteristics of a text file (e.g., frequency of a given word). This write-up introduces the concept of a trie, specifies the API you’re expected to implement, and outlines submission instructions as well as the grading rubric. Please carefully read the entire write-up before you begin coding your submission. Tries A trie is an example of a tree data structure that compactly...

  • Programming Assignment 9 The purpose of this programming project is to demonstrate a significant culmination of...

    Programming Assignment 9 The purpose of this programming project is to demonstrate a significant culmination of most constructs learned thus far in the course. This includes Lists, Classes, accessors, mutators, constructors, implementation of Comparable, Comparator, use of Collections sort, iterators, properly accessing fields of complex objects, and fundamental File I/O. BACKGROUND Look over Programming Project #4 at the end of the Searching & Sorting Chapter (13), page 869. Programming Assignment 9 is similar with some added features as described below....

  • accordingly answer and i pt): Polymers and Monomers. Read each question chemical structure, in a oymer chemical structure based on the polymer name and provide the monomer unit chemical st...

    accordingly answer and i pt): Polymers and Monomers. Read each question chemical structure, in a oymer chemical structure based on the polymer name and provide the monomer unit chemical structure, in addition. Lastly applications. An example answer is provided as guidance. , provide an example of how the polymers are utilized in (bio)engineering Polymer Chemical Structure Polymer Full Name and Abbreviation Usage Example for Monomer Unit Structure(s) _ _pt per box) pt per box) pt per box) Nucleic Acid (DNA)...

  • This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing...

    This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification. As you should know by now, bucket sort works by placing items into buckets and each bucket...

  • I need this in C++. This is all one question. Introduction Your eighth assignment will consist...

    I need this in C++. This is all one question. Introduction Your eighth assignment will consist of two programs, which will involve the use of simple classes. The source code for these problems should be submitted using the naming conventions we specified in class. Please note that your computer programs should comply with the commenting and formatting rules as described in class. For example, there should be a header for the whole program that gives the author's name, class name,...

  • Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in crea...

    Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in creating board-game applications. Later we will use this class to create a board game. A grid is a two-dimensional matrix (see example below) with the same number of rows and columns. You can create a grid of size 8, for example, it’s an 8x8 grid. There are 64 cells in this grid. A cell in the grid...

  • What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that...

    What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways. Polymorphism allows a programmer to use a subclass object in place of a superclass object. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation. Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work. Assume that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT