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

Java question b. s wrong with the following code for interface? What should be c

ID: 3728583 • Letter: J

Question

Java question b. s wrong with the following code for interface? What should be changed to make a valid interface for objects that have colors? public interface Colored private Color color public Color getColor return colori c. Consider the following classes: public class Vehicle public class Car extends Vehicle implements Speed f public class SUV extends Car {…} which of the following are legal statements? i. ii. vehicle v = new Car(); Vehicle v= new SUV ( ); Car c = new SUV ( ); iii. iv. SUV s new SUVO v. SUV s = new Car ( ); vi. Car c new Vehicle) vii. Speed s new Car ) viii. Speed s new SUV)

Explanation / Answer

b)


/**Correct code to set color
* of object that implements the class
* Colored*/
//Colored.java
import java.awt.Color;
public interface Colored {
   //set default value,remove private modifier
   Color color=Color.WHITE;
   //No method body
   public void setColor(Color color);
   //No method body
   public Color getColor();
}

-------------------------------------------------------------------

c)

i.Valid type since Car is inherited from Vehicle
Vehicle v=new Car();
ii.Valid type since SUV is inherited from the Car which inturn inherited from Car
Vehicle v=new SUV();
iii.Valid since SUV is inherited from Car
Car c=new SUV()
iv.Valid since SUV creats an instance of SUV class
SUV= s=new SUV();
v. Invalid type since Car is base class
SUV is child class
upcasting is required
SUV s=new Car();
vi. Invalid type since Vehicle is base class
Car is child class
Car c= new Vehicle();
vii. Valid since Car implements Speed
class interface
Speed s=new Car();

viii. invalid type since SUV didnot implemenet the Speed class
Speed s=new SUV()

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