Link to home
Start Free TrialLog in
Avatar of Swaminathan K
Swaminathan KFlag for India

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);

}
}
Avatar of CPColin
CPColin
Flag of United States of America image

I'm pretty sure you get the error for non-static members only. Nested interfaces are static, so there's no problem.
Avatar of Swaminathan K

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.
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.
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.
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().
}

Open in new window

I want the  example with either Nested Interface or Nested Classes name collision
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
public class CardImpl implements DummyBank.Card {
}

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?
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?
Sorry , I tried to implement the same and it worked.