Results 1 to 6 of 6
- 11-29-2011, 02:33 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
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.
- 11-29-2011, 02:38 AM #2
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.
- 11-29-2011, 02:42 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
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.
- 11-29-2011, 02:49 AM #4
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?
- 11-29-2011, 03:06 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Re: Special Case in a Java Interface?
^^^Just the questions and concepts I was looking for.
That's perfect, thanks!
- 11-29-2011, 01:36 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
java switch case
By aconti in forum New To JavaReplies: 16Last Post: 08-09-2011, 07:05 AM -
Special characters JavaDB into Java
By newbie123 in forum JDBCReplies: 11Last Post: 07-04-2011, 05:24 PM -
Java, i need help with a recursion base case please
By farahm in forum Advanced JavaReplies: 1Last Post: 04-18-2011, 12:27 AM -
Help on Java Input Case Insensitivity
By Suzanne1187 in forum Java AppletsReplies: 1Last Post: 02-10-2009, 09:40 AM -
Is Java Case Sensitive???? Prove It
By j2vdk in forum New To JavaReplies: 53Last Post: 09-01-2008, 09:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks