File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When implementing inheritance do we have to define instance variables

 
Varuna Seneviratna
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


Varuna
 
Thomas Thevis
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Varuna,

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.

Hope this helps,
Thomas
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic