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 5 of 5

Thread: Inheritance and Overriding help!

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Inheritance and Overriding help!

    Hi,
    -->My problem is quite simple.
    -->I have a super class with a method public void add(int theValue)
    -->I have an interface with a method boolean add(int theValue)
    -->My subclass implements ISet which is my interface and extends my superclass
    -->I need to put in my subclass (SetInherit) a method public boolean add(int theValue) but apprently it is illegal to change the return type on and overridden method from the superclass.
    -->I need this boolean add(int theValue) method to check to see if the bag has the value, if it does I need it to return false and do nothing and if the value isn't in the bag I need it to send the value to the superclass and have it added to the bag.
    -->All I need is to know how to implement this method without changing the name. Thank you!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Inheritance and Overriding help!

    Simple: copy-paste the method declaration, erase the abstract keyword (if any), remove the ending semi-colon, add curly braces, and implement the method.

    public Interface MyInterface
    {
        public void doIt();
    }
     
    public class Base implements MyInterface
    {
        // the overriden method doIt
        public void doIt()
        {
            // you're overriden implementation goes here
        }
    }

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance and Overriding help!

    Thanks but unfortunately that's not what I was looking for. It's along the lines but I already know how to do that. See I have the interface that has a boolean add(int theValue) but its "boolean". In the superclass the same method add is void not boolean. So here's my problem.
    public class BoundedBag
    {
        public void add(int theValue)
       {
            //stuff
       }
    }
     
    public interface Iset
    {
       boolean add(int theValue);
      //more methods
    }
     
    public class SetInherit extends BoundedBag implements Iset
    {
         //this is where I need to use public boolean add(int theValue)
        //unfortunately I tried to override using a different return type and it returned an illegal action.
    }
    Of course I left out the default constructors and such.
    Last edited by helloworld922; February 19th, 2011 at 09:56 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Inheritance and Overriding help!

    Ahh, I didn't see that. That's not allowed.

    Quote Originally Posted by Java Language Specification
    8.4.8.4 Inheriting Methods with Override-Equivalent Signatures
    It is possible for a class to inherit multiple methods with override-equivalent (§8.4.2) signatures.

    It is a compile time error if a class C inherits a concrete method whose signatures is a subsignature of another concrete method inherited by C.
    If you are able to modify either one of those classes, I would suggest you either slightly change the name of one, change the return type of one to match the other, or add a "dummy parameter" (this last one isn't really suggested). Otherwise, you cannot both implement that interface and extend the other class.

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Inheritance and Overriding help!

    You should consider throwing an exception instead of returning a boolean as a success indicator (e.g. DuplicateEntryException). It sounds like when the value is already in the bag, the method cannot complete (it fails). This is what exceptions are for, you really shouldn't be using boolean return values to indicate success or failure.

    This does mean that both the Iset method and the BoundedBag method should be declared to throw the exception.

    Incidentally, if the method has the same semantics in Iset and BoundedBag, why doesn't BoundedBag implement Iset?

    If the semantics are different for the two methods, you shouldn't try to make their signatures the same - it's asking for trouble. You should explicitly differentiate them by changing the name of one or both (let's face it, 'add(int)' is likely to cause clashes). Why not addItem(..) or even addBagItem(..), etc.
    Last edited by dlorde; February 24th, 2011 at 01:48 PM.

Similar Threads

  1. Overloading vs Overriding
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:53 AM
  2. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  3. Overriding methods declaration problems
    By TurboTuring in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 9th, 2010, 11:23 AM
  4. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM