Results 1 to 3 of 3
- 12-01-2010, 07:59 PM #1
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
setting attributes(variables) in an Abstract class
Hi All,
I have been reading about and playing with abstract classes and interfaces. I understand how the methods work with respect to implementing an interface or extending an abstract class. What I am trying to understand, though, is how the attributes (or variables) work when extending an abstract class.
For example, lets say I have an abstract class that contains undefined variables. Can I set those variables within the class that is extending the abstract class without using a public method? How would that work, or is it unreasonable to declare variables in a abstract class without giving them values within that class?
Example Code:
I did create a public "setter" method within the abstract class and called that method within the ExampleProgram class and that did work... but I don't want that variable open to being set from outside the ExampleProgram class.Java Code:abstract class Setup extends JInternalFrame{ String programName; JPanel[] panels; String[] panelTitles; public Setup() { //... code that creates the JInternalFrame, etc... // ...instantiates a JTabbedPane and a JScrollPane... } public void addPanels (JPanels[] panels) { //...code to add the panels to a JTabbedPane within the JInternalFrame... } } public class ExampleProgram extends Setup { public ExampleProgram() { // ... I want define the programName, Panels it uses, and the panel // titles here, but I am not sure how ... } }
Anyone have any good tutorial links, books, ideas, suggestions... ?
Thanks in advance for any help!!
Chris:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 12-01-2010, 08:32 PM #2
The tutorial located here may help you in understanding.
However, generally it is best not to access parent class variables directly. If you access such things via methods, you can easily change the implementation of the parent class without having to worry about changing how all the subclasses intereact with the parent class. It is one of the reasons that using polymorphism can be so powerful.
- 12-01-2010, 08:45 PM #3
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Thank you Mr. Beans! I found what I was looking for in your link. (There are still so many things for me to think about when programming that I completely forgot about the other access modifiers!)
I used the protected modifier on my "setter" method which allows the ExampleClass to set the variables but does not allow the class that instantiated ExampleClass to call the setter method!
Thanks again! :)
Chris:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
Similar Threads
-
final variables in abstract classes
By parulmahajan in forum New To JavaReplies: 4Last Post: 06-11-2010, 09:54 AM -
OOP/RTII: Abstract classes with methods using uninitialized instance variables?
By CyJackX in forum New To JavaReplies: 12Last Post: 03-16-2010, 06:45 PM -
setting path and classpath variables from batch file -
By manojsingh.manoj@gmail.co in forum Advanced JavaReplies: 1Last Post: 09-15-2009, 11:50 AM -
JAVA -Setting system variables for Vista
By vertesi in forum New To JavaReplies: 3Last Post: 03-20-2009, 08:10 AM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 10:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks