Open chanavigail opened 2 years ago
Yeah I'm quite confused by it too. Does making a variable protected so that a child can assess it break abstraction?
Using protected will allow your child classes to call upon your parent class' attributes. This correspondingly allows you to super() in the constructor method for your child classes. However, CS2030 does not allow the use of protected as you will fail checkstyle.
Does making a variable protected so that a child can assess it break abstraction?
Yeah, because if those variables names change, then you have to go through every clients code to change the variable name. Vs if you implement a protected getter method in the parent, and call this getter method in client code, then you can just update the getter method's code(keeping the method signature/name the same) whenever the underlying variable name(s) change, without changing the client code(unless you decided to rename the getter method). This is the 'abstraction barrier', because the client doesn't have to care what the actual variables are called, just what method to call to obtain a specific value(desirable property), and ideally this method (signature) doesn't change, only the actual variables(i.e. what to do within the method body) do
Does this mean we cannot use "protected" key word at all at any point in the module (PA included)? I'm a bit confused as the use of "protected" was introduced in the lectures but it seems like we are not allowed to use it anywhere.
Does this mean we cannot use "protected" key word at all at any point in the module (PA included)? I'm a bit confused as the use of "protected" was introduced in the lectures but it seems like we are not allowed to use it anywhere.
Yes, even though we were taught the protected concept, this is mainly for completeness of understanding and learning. We are not allowed to use protected during practical exams.
Does this mean that for CS2030 we can only use "private" and in order to access the private variables we should simply use super (for sub-classes only ofc)?
Does that mean we can only use getters method in the parents class so that we can get the parents' properties values from the child class?
Using protected will allow your child classes to call upon your parent class' attributes. This correspondingly allows you to super() in the constructor method for your child classes. However, CS2030 does not allow the use of protected as you will fail checkstyle.
Just to add a qualifier, I think that the adding a protected modifier only fails checkstyle if you add the protected modifier to a variable. But if you add protected modifier to a method of the class (say a getter), then check style will let you through, so you can use getters to allow your child class to access your parent class' private variables. Some may argue that you shouldn't do that because it breaks abstraction but honestly ... it depends on your implementation. Sometimes you'll need to make some trade-offs between a type of abstraction for another depending on your implementation, so just do what works for you!
Does that mean we can only use getters method in the parents class so that we can get the parents' properties values from the child class?
Does this mean that for CS2030 we can only use "private" and in order to access the private variables we should simply use super (for sub-classes only ofc)?
Yup! Just for the purposes of this mod, all attributes of your class must be declared "private final" instead of "protected" so instead you should have getters in the parent classes and use those if the subclasses need to access any attributes. But outside of this module of course, simply declaring your attributes "protected" would be much less of a hassle:-)
protected class is used such that only the child class of that parent can access its attributes while other classes are unable to access the protected attributes. however, for private attributes, even the children cannot access them(so a getter is used instead). As @celine-chung mentioned, protected class would be more convenient since you don't have to create a getter method every time you want the child to access the parent's attributes. However, for this module, it is insisted that we do not use protected at all (only use private), for best coding practices or smth.
you can reference to this as well
Usually when we form constructors for our classes, we use the 'private' keyword for our variables. But this becomes an issue when we form child classes and have to refer to these variables in other methods of the child class. What would the difference between using 'protected' so that we can still reference these variables, versus implementing getters in the parent class? Is this an issue functionality-wise, or just a convention to follow?