20.9k views
3 votes
Can you please answer part 1 and part 2 for me. I am studying for a test and want to understand my reveiw.

Part 1

Shirt inherits Clothing. Clothing has the fold()method, but Shirt does not. Which fold() is executed in the following code?

Shirt myShirt = new Shirt();

myShirt.fold();
1. The Shirt version

2. This results in a compiler error.

3. This results in a runtime error.

4. The Clothing version

Can you tell my why 4 is correct and why all the others are incorect.

Part 2

The class Shirt extends the Clothing class. To make the class DressShirt inherit the functionality of both Clothing and Shirt its class header would be:

Can you tell me why 1 is correct and the others are incorrect

1. public class DressShirt extends Shirt

2. public class DressShirt inherits Shirt, Clothing

3.public class DressShirt extends Shirt, Clothing

4. public class DressShirt inherits Shirt

1 Answer

1 vote

Part 1:

The correct answer is 4. The Clothing version of the fold() method is executed in the given code.

Step-by-step explanation:

Inheritance allows a subclass to inherit the properties and methods of its superclass. In this case, the Shirt class is a subclass of the Clothing class.

When an object of the Shirt class is created (`Shirt myShirt = new Shirt()`), it inherits the fold() method from the Clothing class. Therefore, when the `myShirt.fold()` statement is executed, it calls the fold() method of the Clothing class, not the Shirt class.

The other options are incorrect:

1. The Shirt version: This is incorrect because the Shirt class does not have its own version of the fold() method. It inherits the fold() method from the Clothing class.

2. This results in a compiler error: This is incorrect because there are no syntax errors in the code. The Shirt class inherits the fold() method from the Clothing class, so the code compiles successfully.

3. This results in a runtime error: This is incorrect because there are no runtime errors in the code. The fold() method is inherited and can be called without any issues.

Part 2:

The correct answer is 1. The class header to make the class DressShirt inherit the functionality of both Clothing and Shirt would be `public class DressShirt extends Shirt`.

Step-by-step explanation:

To make a class inherit the functionality of both Clothing and Shirt, the class header should use the `extends` keyword to inherit from Shirt. Since the Shirt class already inherits from the Clothing class, DressShirt automatically inherits the functionality of both classes.

The other options are incorrect:

2. `public class DressShirt inherits Shirt, Clothing`: This is incorrect syntax. In Java, you can only specify one superclass using the `extends` keyword.

3. `public class DressShirt extends Shirt, Clothing`: This is also incorrect syntax. Multiple inheritance is not allowed in Java, so you cannot directly inherit from multiple classes.

4. `public class DressShirt inherits Shirt`: This is incorrect because the `inherits` keyword is not valid in Java. The correct keyword to indicate inheritance is `extends`.

Therefore, the correct class header to make DressShirt inherit from both Clothing and Shirt is `public class DressShirt extends Shirt`.

User RET
by
8.6k points

Related questions

asked Dec 12, 2024 133k views
Hughjdavey asked Dec 12, 2024
by Hughjdavey
8.3k points
2 answers
0 votes
133k views
asked Feb 25, 2024 117k views
Soosus asked Feb 25, 2024
by Soosus
8.0k points
1 answer
0 votes
117k views