FUNDAMENTAL QUESTION 2:

G

Gary

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());
}
}
 
V

VisionSet

Gary said:
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());
}
}

Type and class are two different things, in both cases the class is
TestClass, so they will both behave identically, it is just that in the 1st
case the Type is TestInterface, so the only *accessible* methods are those
defined in the interface.
 
H

hexathioorthooxalate

Gary, in your example both tc1 and tc2 are an instance of a class. But if
you modify your programme there is a subtle difference in functionality; tc1
exposes the public methods in the interface, tc2 exposes these and others.
Regards
Hexathioorthooxalate


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 String getName2()
{
return "Mr. "+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(tc1.getName2()); //illegal
System.out.println(tc2.getName());
System.out.println(tc2.getName2());
}
}
 
B

Bjorn Abelli

...
I would have expected that tc1 would
have been an interface
and therefore have no implementation.
TestInterface tc1 = new TestClass("Gary1");

tc1 is neither a "class" or an "interface".

tc1 is a "reference variable" of type TestInterface.

That variable can hold a reference to an instance of any class that has
implemented that interface. In this case an instance of TestClass.

The interface doesn't have any implementation, but TestClass does. By
implementing the TestInterface, TestClass has made a "contract" on having an
implementation for the methods declared in the interface.

When you call tc1.getName(), you're sending the message to the *instance*
regardless of type of the variable.

Look up on "polymorphism".

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top