Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Oshedhe Munasinghe
8,108 PointsSubtype?
What does subtype mean?
is it:
Dog doggie = new Dog();
doggie - called reference varible, right? Dog - subtype?
I'm confused
3 Answers
jcorum
71,830 PointsSubtype is sometimes used as a synonym for subclass.
In the statement: Dog doggie = new Dog(); Dog is the type or class doggie is the name of the object that is being created new is the keyword for creating objects Dog() is the constructor
If you create a subclass that extends the Dog class, say Retriever: public class Retriever extends Dog {...} then Retriever would be the subclass and Dog the superclass
If you create a Retriever object this way: Dog doggie2 = new Retriever(); then the object doggie2 would be a Dog at compile time but a Retriever at run time and Retriever could be called the subtype, as well as the subclass.
jcorum
71,830 PointsIn Java executing code is a two-step process. First you go from text to byte code, That's called compiling. Second you go from byte code to machine code. That's called running.
In most IDEs there are separate buttons: one to compile, one to run.
When you have one class Pug extend another class Dog, then Dog is the super class and Pug is the sub class, and Pug inherits the public methods of Dog. This sets up an inheritance hierarchy.
As you will see in most text books, inheritance models "is-a" relationships: a pug is a dog. So you can do this (assuming neither Dog nor Pug is abstract): Dog d1 = new Dog(), or Pug p = new Pug(). You can even do this: Dog d2 = new Pug()
But since a dog is not necessarily a pug, you cannot do this: Pug p = new Dog(). As you point out, the compiler will complain.
Your bark() method is called polymorphic, because it has (in your example) two forms. When the code d2.bark() runs it will use the overridden version, because at run time d2 is a Pug.
Oshedhe Munasinghe
8,108 Points ty very much
Oshedhe Munasinghe
8,108 PointsYo jcorum!! Thanks!
Let's say I have created two classes Dog and Pug. Pug extends Dog (IS-A).
Dog class methods: bark(), poop(), run() Pug class methods: @override bark() , walk(), dance()
So you mean when I call Pug pug = Dog(); the subclass Pug will call as supertype, am I correct? But what do you mean compile time and run time?? I don't understand these two can you please explain?
Also can you explain me why I get an error when I call: Pug dog = new Dog();
I get warning "Incompatible type" from compiler. And I understood that when I call dog.(I can't get any methods) is it because Dog is a superclass "abstract"?