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 trialolu adesina
23,007 Pointsi know the answer but i do not know Why BUT
the answer is A BUT WHY
Which of these code expressions evaluates to true?
Choose the correct answer below:
A new Square(2.5) is Polygon
B new Polygon(4) is Square
1 Answer
andren
28,558 PointsA Polygon simply put is a two dimensional shape formed with straight lines, a Square is a type of polygon.
A square can be considered a polygon because it matches the definition of what a polygon is. You cannot however say that a polygon is a square, because there are many shapes that classify as a polygon like triangles, rectangles, etc.
So all squares are polygons, but not all polygons are squares.
A simpler analogy is that of animals. You can say that a cat is an animal, but you cannot say that an animal is a cat, because the term animal includes a wide range of different creatures, not just cats.
If we take a step back from analogies and focus more on the programming aspect, if you have a class called Square
that inherits from a class called Polygon
then the Square
class will have all of the properties and methods that belong to the Polygon
class. That is a requirement of inheriting from it.
Because of that a Square
can safely be treated as if it actually was a Polygon
without it causing any issues. Treating a Polygon
as if it were a Square
however would not work, because a Square
could have methods and properties that are not found in the Polygon
class.