I am trying to override setColour in the subclass callled ObjectB in the public void setColour(Colour xColour) method. So the OBjectB cannot change to purple. Can you see where I have gone wrong because the ObjectB is currently changing to purple colour. Please reply only if you know the answer. I know its in the public void setColour(Colour xColour) method but I don't know how to change the code.

Thanks in advance.

public class ObjectA
{
  private Colour acolour;

}

public ObjectA()
{

 acolour = PURPLE;
}

public Colour getColour()
{
      return acolour;
}


public void setColour(Colour xColour)
{
     acolour = xColour;
     update("acolour");
      
}

public void purple()
{
     setColour(Colour.PURPLE);
}


public void sliver()
{
     setColour(Colour.sliver);
}

public void orange()
{
     setColour(Colour.orange);
}

public class ObjectB extends ObjectA
{
    colourY = Colour.GOLD;

}

 public Colour getColour()
  {
   return colourY;
  }

public void setColour(Colour xColour)
   {
       colourY = xColour;
       this.update("colourY");
   }


public void blue()
   {
      setColour(Colour.BLUE);
   
   }
   
    public void GREY()
   {
      setColour(Colour.GREY);
  
   }
   
    public void gold()
   {
      setColour(Colour.GOLD);
   }

Recommended Answers

All 2 Replies

Inside the overridden method use code that checks the color. If it is purple, don't change it

thank you - solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.