Results 1 to 3 of 3
  1. #1
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default 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:
    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 ...   
       }
    }
    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.

    Anyone have any good tutorial links, books, ideas, suggestions... ?

    Thanks in advance for any help!!
    Chris

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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.

  3. #3
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    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

Similar Threads

  1. final variables in abstract classes
    By parulmahajan in forum New To Java
    Replies: 4
    Last Post: 06-11-2010, 09:54 AM
  2. Replies: 12
    Last Post: 03-16-2010, 06:45 PM
  3. setting path and classpath variables from batch file -
    By manojsingh.manoj@gmail.co in forum Advanced Java
    Replies: 1
    Last Post: 09-15-2009, 11:50 AM
  4. JAVA -Setting system variables for Vista
    By vertesi in forum New To Java
    Replies: 3
    Last Post: 03-20-2009, 08:10 AM
  5. Replies: 6
    Last Post: 02-11-2009, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •