Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: subclassing DefaultMutableTreeNode Class

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default subclassing DefaultMutableTreeNode Class

    I have defined a subclass of DefaultMutableTreeNode Class in java :

    public AccountsTreeNode extends DefaultMutableTreeNode {

    }

    in this class I have a recursive method called getsorted(ArrayList list_) , which accummulated the list_.

    In this method, I have to call this.children() which returns the children of the node, but the children
    are all of type "DefaultMutableTreeNode". I have to call the method getsorted on these children,
    but since the getsorted is a mehtod of AccontsTreeNode, it gives me runtime cast error.

    What is the best solution to these problem? Should I convert children to AccountTreeNode type by
    overriding the children() method?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: subclassing DefaultMutableTreeNode Class

    Quote Originally Posted by parviin57 View Post
    I have defined a subclass of DefaultMutableTreeNode Class in java :

    public AccountsTreeNode extends DefaultMutableTreeNode {

    }

    in this class I have a recursive method called getsorted(ArrayList list_) , which accummulated the list_.

    In this method, I have to call this.children() which returns the children of the node, but the children
    are all of type "DefaultMutableTreeNode". I have to call the method getsorted on these children,
    but since the getsorted is a mehtod of AccontsTreeNode, it gives me runtime cast error.

    What is the best solution to these problem? Should I convert children to AccountTreeNode type by
    overriding the children() method?
    Class cast each child to the appropriate class

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  5. Subclassing Singleton class
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: December 22nd, 2011, 12:42 AM