Results 1 to 2 of 2
  1. #1
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Interface trouble, cannot override method

    Hi everybody, I'm having some trouble with an interface and the implementation of said interface.

    Here be the interface:
    Java Code:
    /**
     * The interface BinaryTree is used to
     * implement a number of well defined 
     * methods which makes handling a tree
     * considerably easier.
     * 
     * @author (Tom) 
     * @version (Alpha)
     */
    public interface BinaryTree<T>
    {
        /**
         * Returns a specific node from the BinaryTree.
         * If the object isn't found the method returns null.
         * 
         * @param  obj    object to find 
         * @return        the requested object
         */
        T findNode(T obj);
    
        /**
         * Returns the number of nodes in the tree
         * 
         * @param  y    a sample parameter for a method
         * @return        integer 
         */
        int size();
        
        /**
         * Returns the number of leaves in the tree
         * 
         * @return        integer 
         */
        int numberOfLeaves();
    }
    And here's the main class:

    Java Code:
    /**
     * Write a description of class Tree here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Tree<T> implements BinaryTree
    {
        // instance variables - replace the example below with your own
        private int x;
    
        /**
         * Constructor for objects of class Tree
         */
        public Tree()
        {
            // initialise instance variables
            x = 0;
        }
    
        /**
         * Returns a specific node from the BinaryTree.
         * If the object isn't found the method returns null.
         * 
         * @param  obj    object to find 
         * @return        the requested object
         */
        public T findNode(T obj){
            return null;
        }
        
        /**
         * An example of a method header - replace this comment with your own
         * 
         * @param  y    a sample parameter for a method
         * @return        the result produced by sampleMethod 
         */
        public int size(){
            return 42;
        }
        
        /**
         * Returns the number of leaves in the tree
         * 
         * @return        integer 
         */
        public int numberOfLeaves(){
            return 42;
        }
    }
    The error code given is: Tree is not abstract and does not override abstract method findNode(java.lang.Object) in BinaryTree.

    What is wrong with my implementation of the findNode method?
    //T.S

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Interface trouble, cannot override method

    Your class Tree<T> is stated to implement BinaryTree -- not BinaryTree<T>. The method signature should be
    Java Code:
    public Object findNode(Object obj)
    or the class should implement the parametrized interface.

    In future, please remove inane and irrelevant comments before posting your code here. They only add clutter. And adopt a consistent bracing style.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Override toString() Method
    By Apparition78 in forum New To Java
    Replies: 1
    Last Post: 02-03-2012, 10:32 PM
  2. Can we override static method?
    By srinivasmallabathula in forum Advanced Java
    Replies: 3
    Last Post: 06-24-2011, 02:29 PM
  3. Override class method
    By Mekie in forum New To Java
    Replies: 8
    Last Post: 11-01-2010, 06:26 AM
  4. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  5. Override Entity Callback Method
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-13-2009, 06:05 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
  •