开发者

Question On OOPs

开发者 https://www.devze.com 2023-03-24 17:09 出处:网络
public class Car { public char color; public char getColor() {开发者_C百科 return color; } public void setColor(char color)
public class Car 
{
  public char color;

  public char getColor()
  {   开发者_C百科     
    return color;    
  }    

  public void setColor(char color)
  {       
    this.color = color;    
  }
}

public class MyCar
{  
    private Car car = null;     

    public MyCar()
    {
        this.car = new Car();
        car.color = 'R';
    }
}

Which OOPS principle does the above code violate?

• Abstraction • Encapsulation • Polymorphism • None of the above

I understand that Encapsulation is the answer to this problem. Just wanted to know if other option is also true.


Well, I would view it in these terms:

  • Encapsulation: by allowing direct access to the color field, the Car class is exposing an implementation detail. Ignacio has shown that he doesn't view this type of violation as one of encapsulation, but of data hiding - my own view of the word "encapsulation" is that it includes data hiding. That just goes to show how these words can be used in different ways.

  • Polymorphism: judging by the names MyCar and Car should potentially implement a common interface or have a common base class. At least, the classes given can't be used polymorphically.

  • Abstraction: I would argue that using char as the abstraction for a color is inappropriate. Whether that's an abstraction violation or not again depends on what you mean by "abstraction violation".


If public char color; was instead private char color; it would be an example of encapsulation. This is because while in your current version of the code the value is set immediately, that is an implementation detail. For instance, you could decide to add validation - for example only allowing upper case chars. By allowing the field color to be set directly that validation could not be performed.


Information hiding is violated in Car.

I think other principles are formally respected, ie. nothing stops me from having a MyCar class wrapping the Car class if the two classes are not intended to be used polymorphically.

One could argue that it is just bad design, and I would agree.

The same applies to using char as a color: it's probably bad design, but IMHO nothing formally against OOP principles.


As per my Understanding "Data Hiding" is violated.As you are exposing the internal details of the Car class(color), also you are making it public exposed and any one can modify it with Valid or In Valid values. Now look at Encapsulation OOP principle : Encapsulation: It is not violated, because related data is kept in a cleanly closed container here it is class and Car information are not scattered here and there.


As per OOPs Concept, your code is violated Data Hiding. Because because you are declaring public variable at class level and using it in method same class also. And if you are violated the Data Hiding means you also violating the Encapsulation.

public class Car 
{
   public char color;

   public char getColor()
   {        
     return color;    
   }    

   public void setColor(char color)
   {       
     this.color = color;    
  }
 }

In above code, you are violating the Data Hiding Concept. Because color variable is directly accessible out side the class. Encapsulation is also violated by above code.

0

精彩评论

暂无评论...
验证码 换一张
取 消