0

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:

  1. ClassThatImplements
  2. ClassThatIsInterface
  3. 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?

Thomas
  • 6,291
  • 6
  • 40
  • 69

1 Answers1

1

There are three ways you can look at inheritance in general

  • inheritance of state (the internal representation of a class' instances)
  • inheritance of behavior (in this case, the methods and their implementations)
  • inheritance of type (this is what constitutes an is-a relationship)

The first two are closely tied together and often not even considered separately.

The two keywords in Java that help you enforce these types of inheritance are extends and implements.

The former only allows a single line of inheritance and by using it, you create an inheritance relationship of all of the three types mentioned above.

The latter, on the other hand, allows you to use multiple inheritance, which is, however, limited to the inheritance of type. Multiple inheritance of state and behavior is not possible in Java and composition should be used to achieve similar effects instead.

This is basically how Java was designed as a language. If you take a look at other (more or less) object-oriented languages, you can see other approaches. C++ supports multiple inheritance in all three aspects (which IMO is not really a good thing). Ruby allows you to handle the inheritance of behavior by using mixins, also, less attention is paid to the enforcement of types as the language supports duck typing.

So, here is my question: conceptually, what is happening to ClassThatImplements to where it is recognized as of type ClassThatIsInterface?

To sum up, the fact that a class implements an interface alone defines an is-a relationship between the two. There's nothing more happening under the hood.

You can take a look at the exact rules of sub-typing in the Java Language Specification. In your case, it's pretty straightforward. It gets more interesting when you take a look at generic classes. It's not the most pleasurable read though.

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131