Return to Video

House Equals New Dog - Intro to Java Programming

  • 0:00 - 0:05
    The answer is Error: incompatible types. A Dog is not a kind of House. If you
  • 0:05 - 0:09
    try to store a Dog in a variable that's made to hold a House, you'll get a
  • 0:09 - 0:15
    compile time error, which is good. If Java let you store a Dog in a House
  • 0:15 - 0:21
    variable, you might then try to call House methods on the Dog. And the Dog
  • 0:21 - 0:25
    wouldn't know how to do them. Getting this error is actually a good thing. It
  • 0:25 - 0:28
    means that java is helping us. This is one of the things that we were talking
  • 0:28 - 0:32
    about when we mentioned really early on that java is a safe language. If java
  • 0:32 - 0:36
    let you create a Dog and then store it as a House, you might try to live in it
  • 0:36 - 0:41
    later and fail. But instead of doing that, the compiler will immediately tell
  • 0:41 - 0:44
    you that there's something wrong with your code. Other languages might allow
  • 0:44 - 0:47
    you to keep running the code, and then mysteriously fail or return the wrong
  • 0:47 - 0:52
    results. This third option isn't quite what we want. This the area that you
  • 0:52 - 0:57
    would get if Dog were an interface. If you tried to make a new Drawable, then
  • 0:57 - 1:01
    you would get an error like this. Error Drawable is abstract cannot be
  • 1:01 - 1:06
    instantiated. The Drawable interface has methods sort of, but it doesn't have
  • 1:06 - 1:12
    enough detail about them for Java to actually make an object Drawable for you.
House Equals New Dog - Intro to Java Programming

more » « less
Video Language:
cs046: Intro to Programming

English subtitles

Revisions Compare revisions