Swaminathan K
asked on
mULTIPLE iNHERTANCE FOR mEMBER TYPES IN iNTERFACES jAVA
hI ,
Iam trying to implement the issues we get when we inherit two interfaces with same member type name , I should get the member type name ambiguity error , but not getting it. Any help is really appreciated.
public interface Bank
{
interface Card
{
String getCardType();
}
}
public interface ATM
{
boolean login (int acctno);
interface Card
{
double getamt(int acctNo);
}
}
public interface DummyBank extends ATM, BANK
{
int getChequeDepositedDetails( int acctno);
}
-- Should produce an error for member type name when compiled ambiguity , but not getting it.
public class SBIBankImpl implements DummyBank
{
public String getCardType()
{
return "SBI DEBIT CARD";
}
public boolean login (int acctno)
{
return true;
}
public double getamt(int acctNo)
{
return 10000.03;
}
public int getChequeDepositedDetails( int acctno)
{
return 1233444444;
}
public static void main (String [] args)
{
SBIBankImpl c = new SBIBankImpl();
String name=c.getCardType();
System.out.println(name);
}
}
Iam trying to implement the issues we get when we inherit two interfaces with same member type name , I should get the member type name ambiguity error , but not getting it. Any help is really appreciated.
public interface Bank
{
interface Card
{
String getCardType();
}
}
public interface ATM
{
boolean login (int acctno);
interface Card
{
double getamt(int acctNo);
}
}
public interface DummyBank extends ATM, BANK
{
int getChequeDepositedDetails(
}
-- Should produce an error for member type name when compiled ambiguity , but not getting it.
public class SBIBankImpl implements DummyBank
{
public String getCardType()
{
return "SBI DEBIT CARD";
}
public boolean login (int acctno)
{
return true;
}
public double getamt(int acctNo)
{
return 10000.03;
}
public int getChequeDepositedDetails(
{
return 1233444444;
}
public static void main (String [] args)
{
SBIBankImpl c = new SBIBankImpl();
String name=c.getCardType();
System.out.println(name);
}
}
I'm pretty sure you get the error for non-static members only. Nested interfaces are static, so there's no problem.
ASKER
Hi
We will get an error when we have two interfaces with member types with same name even though they are static or non static , I should get ambiguity error.
We will get an error when we have two interfaces with member types with same name even though they are static or non static , I should get ambiguity error.
ASKER
public interface Bank
{
int Yes=1;
}
public interface ATM
{
int Yes=2;
}
public interface DummyBank extends ATM, BANK
{
int ans=Yes;
}
Throws as ambiguity error telling which interface constant value to use. Similar to this I should get an error for member types in Interface.
{
int Yes=1;
}
public interface ATM
{
int Yes=2;
}
public interface DummyBank extends ATM, BANK
{
int ans=Yes;
}
Throws as ambiguity error telling which interface constant value to use. Similar to this I should get an error for member types in Interface.
Well, sure, in that example, the compiler has no way of knowing whether you mean Bank.Yes or ATM.yes. In your first example, you're not making any references to Bank.Card or ATM.Card, so there's no problem.
ASKER
Yes, I need an example which will throw an error for the member types , any help in this regard is really appreciated .
interface A
{
default int getValue()
{
return 0;
}
}
interface B
{
default int getValue()
{
return 1;
}
}
class AB implements A, B
{
// This produces an error because the compiler can't tell if you want
// A.getValue() or B.getValue() to run when somebody calls AB.getValue().
}
ASKER
I want the example with either Nested Interface or Nested Classes name collision
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I want the example with either Nested Interface or Nested Classes name collision
I can't think of one. It seems unlikely to happen in real code.
ASKER
public class CardImpl implements DummyBank.Card {
}
Iam not getting the error , its says cannot find the symbol.
}
Iam not getting the error , its says cannot find the symbol.
Have you got that in a different package or anything like that ? Also what version of Java are you compiling with?
ASKER
Java 1.8 version
Hmmm, not sure then. Can you post the full error? Maybe a screenshot so we know exactly what it is referring to, etc?
ASKER
Sorry , I tried to implement the same and it worked.