Results 1 to 6 of 6
  1. #1
    justinm231 is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Special Case in a Java Interface?

    A) I have two java interfaces: i1 and i2

    B) Interface i1 contains int num = 10; ...while Interface i2 contains num = 20;

    C) I then have an abstract class "TestClass" that implements i1 and i2.

    D) If a TestClass method printOut() says: System.out.print(num); which num will be called since both are inherited?


    Yes, I tested this in Java and it gives me an error: "num is ambiguous". I see clearly what is wrong with the inheritance and (I guess) I see how to fix it.

    Is it correct to just say System.out.print(i1.num); ?

    Can you please give me some discussion on this? I want to know how this should work in more complex situations!





    Thank you, here is super simplified example code below, to demonstrate what I'm doing:

    public interface i1 {
    int num = 10;
    public void delete();
    }

    public interface i2 {
    int num = 20;
    public void add();
    }

    public abstract TestClass implements i1,i2 {
    System.out.print(i)
    //or to correct --> System.out.print(i1.num); <--- ****** This part is what I am looking at ******

    public void add(){
    //Adds something non-applicable.
    }
    public void delete(){
    //May delete that non-applicable something.
    }
    }
    Last edited by justinm231; 11-29-2011 at 02:37 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Special Case in a Java Interface?

    It depends upon what you are actually trying to do. It may simply be a matter of an incorrect design.

  3. #3
    justinm231 is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Special Case in a Java Interface?

    Which would be an incorrect design? The ambiguity or inheritance structure? This is purely conceptual, I'm building this project as a learning aid to a tutorial I watched online.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Special Case in a Java Interface?

    There is no inheritance.

    The design flaw could be due to many reasons. Why do you have 2 interfaces? Why do both interfaces have a variable with the same name? Why is you class implementing both interfaces? Should interface 2 extend interface 1?

  5. #5
    justinm231 is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Special Case in a Java Interface?

    ^^^Just the questions and concepts I was looking for.

    That's perfect, thanks!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Special Case in a Java Interface?

    Quote Originally Posted by Junky View Post
    It depends upon what you are actually trying to do. It may simply be a matter of an incorrect design.
    I would argue sticking attributes into an interface is most definitely incorrect design...no ifs or buts.
    Constants (static final), OK...but not attributes.

Similar Threads

  1. java switch case
    By aconti in forum New To Java
    Replies: 16
    Last Post: 08-09-2011, 07:05 AM
  2. Special characters JavaDB into Java
    By newbie123 in forum JDBC
    Replies: 11
    Last Post: 07-04-2011, 05:24 PM
  3. Java, i need help with a recursion base case please
    By farahm in forum Advanced Java
    Replies: 1
    Last Post: 04-18-2011, 12:27 AM
  4. Help on Java Input Case Insensitivity
    By Suzanne1187 in forum Java Applets
    Replies: 1
    Last Post: 02-10-2009, 09:40 AM
  5. Is Java Case Sensitive???? Prove It
    By j2vdk in forum New To Java
    Replies: 53
    Last Post: 09-01-2008, 09:54 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
  •