FUNDAMENTAL QUESTION 2:

From: Gary (garyjhindrew_at_hotmail.com)
Date: 11/29/03


Date: 29 Nov 2003 02:28:19 -0800

Hi Again, I have written the program below and it prints out "Gary1"
and "Gary2". My question is why does this work. The variable tc2 is an
instance of the class with member variable that holds "Gary2". The
variable tc1 is an interface, not a class, but it seems to be behaving
like an instance of class. I know it behaves this way but I do not
know why. I would have expected that tc1 would have been an interface
and therefore have no implementation.
Thank you
Gary Hindrew

package mypackage7;
public class Class6
{
  public interface TestInterface
  {
    public String getName();
  }

  public static class TestClass implements TestInterface
  {
    private String s;
    public TestClass(String s)
    {
      this.s=s;
    }
    public String getName()
    {
      return s;
    }
  }

  public static void main(String[] args)
  {
   TestInterface tc1=new TestClass("Gary1");
   TestClass tc2=new TestClass("Gary2");
   System.out.println(tc1.getName());
   System.out.println(tc2.getName());
  }
}



Relevant Pages

  • Re: Proper design of classes
    ... but it doesn't require you to implement each interface in your ... 3] IList inherits IEnumerable and ICollection ... public string FirstName ... public string PrivateName ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Do you ever use reflection instead of OO?
    ... It's easy in Java to pass interfaces without actually implementing that interface on any object. ... Htmlizer(new HtmlTableIface() { ... public class MyHtmlTableEntry { ... public String toString() { ...
    (comp.lang.java.programmer)
  • Re: interface & abstract class variables
    ... and an interface. ... if you have a concrete class that extends an abstract ... variables have to the concrete classes depends on what you want to do ... public String otherStuff() { ...
    (comp.lang.java.programmer)
  • RE: Newbie Qn - Interfaces as parameters and members
    ... "Mark R. Dawson" wrote: ... > Hi Steve, ... > concrete class that implements the interface, you don't care what type the ... >> public string sToLocal ...
    (microsoft.public.dotnet.languages.csharp)