Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. True or False? It is legal in a superclass for a method to overload a method

ID: 667571 • Letter: 1

Question

1. True or False? It is legal in a superclass for a method to overload a method in a subclass. Explain.

2. Write the Java code to declare a new class Bee which is a subclass of Insect. The noise made by a Bee is "Buzz".


3. Write the Java code to declare a new abstract class Amphibian that implements the MakesNoise interface.


4. Write the Java code to declare a new class Frog which is a subclass of Amphibian. The noise made by a Frog is "Ribbet".


5 Modify the run() method of Main and add some Bees and Frogs to critters. Build your program and verify that it works correctly. Include all of your .java source code files

6. Explain how a local class differs from an inner class.


7. Explain how an anonymous class differs from an inner and local class.

Explanation / Answer

1)

Yes it is overloading, This overloading is happening in case of the class 'C' which is extending P and hence having two methods with the same name but different parameters leading to overloading of method hello() in Class C. However Class P is only able to access one of the methods which is present in its own definition.

2)

Let's suppose we have an Insect class. This class contains two methods: 1) move() and 2)attack().

Now you want to define a Bee class, which is a type of Insect, but have different implementations of attack() and move(). This can be done by using an inheritance design like the following:

3)

/Main Class/

}

Here interface implements in this class.Basically interface inheritances two or more class but here both different class in same method use then both method which way implement in one class. Please check error correct code.

4)

5)Local class:

Inner class:

6) class MyHandler implements ClickHandler, DragHandler, MovedHandler

In the following example, three inner classes will be generated by the Compiler (correct me if I'm wrong).

The only difference is that the classes in example 1 are named and the classes in example 2 are anonymous. Functionally they are the same otherwise.

If you instead declared the class like

Then what you would have would be a static nested class which differs from the inner class in that the former does not have a reference or direct access to the methods of the enclosing class.

  class Insect {          private int size;          private String color;             public Insect(int size, String color) {                  this.size = size;                  this.color = color;          }             public int getSize() {                  return size;          }             public void setSize(int size) {                  this.size = size;          }             public String getColor() {                  return color;          }             public void setColor(String color) {                  this.color = color;          }             public void move() {                  System.out.println("Move");          }             public void attack() {                  move(); //assuming an insect needs to move before attacking                  System.out.println("Attack");          }  }