BACKGROUND AND SET UP
Okay, let's see if I can get this question to "make sense". The following code examples are meant to communicate the principles and reflect the ideas behind the code. If the code in detail is of interest to you, you can find the code, here.
I have read the basics, consulting the Java Tutorials (which is a wonderful resource), good ol' Dietel, and the documentation. To clarify: I am a beginner programmer.
I have three (3) classes:
- ClassThatImplements
- ClassThatIsInterface
- ClassThatDoesSmthg
Here are their definition(s):
The Interface
Simpe stuff really:
public interface ClassThatIsInterface {
public void doSomething(type variable);
}
The Class that Implements the Interface
public class ClassThatImplents implements ClassThatIsInterface {
// Here's the bit I hope to ask about, pt. 1 of 2:
ClassThatDoesSmthg instance = new ClassThatDoesSmthg(this,
appropriateVariable);
}
The Class that Does Something
public class ClassThatDoesSmthg {
// And the second bit, pt. 2 of 2:
public ClassThatDoesSmthg (ClassThatIsInterface variableOne,
type appropriateVariable) {
}
QUESTION
The above code fragments should give you enough information for the question, I think.
Really, the focus of the question is the latter two classes: ClassThatImplements
and ClassThatDoesSmthg
.
ClassThatDoesSmthg
expects as a part of its constructor an object of type interface
class ClassThatIsInterface
; however, what is sent to the constructor is an object of type ClassThatImplements
which implements ClassThatIsInterface
.
Obviously, without the implementation, the new ClassThatDoesSmthg(this, appropriateVariable)
statement would fail (generate an error). What is interesting to me; however, is that when a class implements
an interface
the implementing class is - in this case - considered an is-a ClassThatIsInterface
(at least, that is how it appears). Consequently, the statement succeeds.
So, here is my question: conceptually, what is happening to ClassThatImplements
to where it is recognized as of type ClassThatIsInterface
?