Question

Given the following package contents, write a short program (class Insecttest) that demonstrates the concept of...

Given the following package contents, write a short program (class Insecttest) that demonstrates the concept of polymorphism using the classes provided in JAVA

package insect;

public class Insect {

     public void nickname () {

       System.out.println ("bug");

     }

     }

public class Flying extends Insect {

     @Override

     public void nickname() {

          System.out.println ("flying bug");

     }

     }

public class Jumping extends Insect{

          @Override

          public void nickname() {

               System.out.println ("jumping bug");

          }

          }

Homework Answers

Answer #1

The answer to this question is as follows:

The polymorphism is of two types:

1.static polymorphism(Method overloading)

2.Dynamic polymorphism(Method overriding)

From the above question, Dynamic polymorphism is implemented

I am trying to execute all the classes at a time so I removed public for the classes if you want to execute you can save as files like insect.java , flying.java , jumping.java

The code is as follows:

class Insect
{//Declaring the class Insect it is the base class for the remaining

public void nickname()
{
System.out.println("bug");
}
}
class Flying extends Insect
{ //decalring the class Flying that is inherited from the Insect class
@Override //overriding the insect class method
public void nickname()
{
System.out.println("flying bug");
}
  
}
class Jumping extends Insect
{//decalring the class Jumping that is inherited from the Insect class
@Override//overriding the insect class method
public void nickname()
{
System.out.println("jumping bug");
}
  
}

public class Test{

public static void main(String[] args){
//Overiding the method that is insect with the method in Flying
Insect insect1=new Flying();
insect1.nickname();
//Overiding the method that is insect with the method in jumping
Insect insect2=new Jumping();
insect2.nickname();
//calling the method in the insect class
Insect insect3=new Insect();
insect3.nickname(); // prints Vehicles can move!!

}

}

Know the answer?
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
Similar Questions
What is the output of the following Java program? class Food { Food() { System.out.println(flavor); }...
What is the output of the following Java program? class Food { Food() { System.out.println(flavor); } String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; } public class Lunch { public static void main(String[] args) { Food lunch = new Pepper(); } } Select one: a. spicy b. the program does not compile c. bland spicy d. no output e. bland
Based on Movie package example, you will create a package about your favorite TV series and...
Based on Movie package example, you will create a package about your favorite TV series and provide the plot() Readme.txt: Explain how you apply polymorphism in your program package Movie; public class MovieTester { public static void main(String[] args) { for(int i = 1; i < 11; i++){ Movie movie = randomMovie(); System.out.println("Movie # " + i + ": " + movie.getTitle() + ", Genre: " + movie.getGenre() + '\n' +"Plot: " + movie.plot()); } } public static Movie randomMovie(){...
What is the output of the following Java program? class Food { String flavor = "bland";...
What is the output of the following Java program? class Food { String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; Pepper(String flavor) { this.flavor = flavor; } } public class Lunch { public static void main(String[] args) { Pepper pepper = new Pepper("sweet"); System.out.println(pepper.flavor); } } Select one: a. bland b. the program does not compile c. no output d. spicy e. sweet
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class Test { int a, b; Test(int i, int j) { a = i; b = j; } void meth(Test s) { s.a *= 2; s.b /= 2; } } class demo { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("Before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("After call: " + ob.a + " " +...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT