When implementing inheritance do we have to define instance variables
Joined: Jan 15, 2007
Since we make the instance variables private in order to implement Encapsulation, When implementing inheritance do we have to define even though the same, instance variables in the subclass or what is the recognized custom?
Joined: Sep 02, 2008
as always it depends on what you're planning to do. In very rare cases you will make the variables in the super class protected instead of private and let overriding classes use them directly. If you allow access through getters and setters, your overriding classes can use these methods. Alternatively, you could implement all state and behavior regarding those variables by an abstract class and let other classes extend the abstract class. Another approach is to use delegation instead of inheritance. Implement the state and behavior associated with the variables by a class and let other classes use this one for state manipulation. Re-defining the variables in overriding classes could also be a valid choice. However, as stated before, the design choice should depend on a specific problem/task.