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

Java please 1) Method overloading public int foo (int x, String y) {...} Which o

ID: 3816063 • Letter: J

Question

Java please

1) Method overloading

public int foo (int x, String y) {...}

Which of the following methods is NOT allowed to be defined in the same class A? Choose the letter in front of All that apply and please explain why you chose it.

a) public int foo(int x, int y) {...}

b) public int bar(int x, String y) {..}

c) public int foo(int x, String y, char z) {...}

d) public int foo(int a, String b) {...}

e) public int foo(int x) {...}

f ) public int foo(String y, inx x) {...}

2) Given this class:

public class Foo {

private int x;

public Foo(int arg) {x = arg; }

public void expand() { x = x + x; }

}

Create a child calss named bar, with a constructor assigning all fields, that overrides expand, causing it to quadruple x's value.

Explanation / Answer


1) Method overloading
public int foo (int x, String y) {...}

Which of the following methods is NOT allowed to be defined in the same class A?
Choose the letter in front of All that apply and please explain why you chose it.
a) public int foo(int x, int y) {...}
b) public int bar(int x, String y) {..}
c) public int foo(int x, String y, char z) {...}
d) public int foo(int a, String b) {...}
e) public int foo(int x) {...}
f ) public int foo(String y, inx x) {...}


Ans:
   public int foo (int x, String y) {...}:
       This method takes two parameters:
           first parameter is int
       and second parameter is String,

       So we can not define others methods with same name that takes two parameters : int and String in same order.

   b) public int bar(int x, String y) {..}
   d) public int foo(int a, String b) {...}


  

2) Given this class:
public class Foo {
private int x;
public Foo(int arg) {x = arg; }
public void expand() { x = x + x; }
}
Create a child calss named bar, with a constructor assigning all fields, that overrides expand,
causing it to quadruple x's value.

public class Bar extends Foo {
public Foo(int arg) {
    super(arg); // calling parent constructor
}
// this is overriden method
public void expand() {
    // calling parent class's expand method twice to make x value quadruple
    super.expand();
    super.expand();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote