Tonight's training was a couple of hours of being introduced to Constructor Methods, in particular, pertaining to Squares, Rectangles and returning the area's of each (we were sending the variables for length and width).
Sometimes the term 'constructor' becomes intimidating. Really, it's just the special method that a class has that people call in order to 'create' or 'construct' an instance of a class.
So, if you have a class named Rectangle, it has a constructor with the name Rectangle, and if that constructor takes two 'int' arguments, anyone could create an instance of that Rectangle with the following code:
Rectangle r1 = new Rectangle(2,3);
We also talked about 'specialization' and 'abstraction.' Really, we talked about the extends keyword, and it indicates that a 'subclass' that extends a 'parent class' is a 'special type of that parent class.'
In terms of our example, we said:
public class Square extends Rectangle {}
The meaning of that is that a Square is a 'special type' of Rectangle. Is the opposite true though? Is a Rectangle a special type of Square? Talk amongst yourselves...
To argue the question, let's look at Wiki's of definitions of a square and rectangle (it's been a long, long time since highschool . . . . lol).
Square:
A square is both a rhombus (equal sides) and a rectangle (equal angles) and therefore has all the properties of both these shapes, namely:
* The diagonals of a square bisect each other and meet at 90 degrees. * The diagonals of a square bisect its angles. * The diagonals of a square are perpendicular. * Opposite sides of a square are both parallel and equal. * All four angles of a square are equal. (Each is 360/4 = 90 degrees, so every angle of a square is a right angle.) * The diagonals of a square are equal.
Rectangle:
In Euclidean plane geometry, a rectangle is any quadrilateral with four right angles. The term "oblong" is occasionally used to refer to a non-square rectangle.
Rhombus:
Every rhombus has two diagonals connecting opposite pairs of vertices and two pairs of parallel sides. Using congruent triangles, one can prove that the rhombus is symmetric across each of these diagonals. It follows that any rhombus has the following properties:
1. Opposite angles of a rhombus have equal measure. 2. The two diagonals of a rhombus are perpendicular; that is, a rhombus is an orthodiagonal quadrilateral. 3. Its diagonals bisect opposite angles.
The first property implies that every rhombus is a parallelogram. A rhombus therefore has all of the properties of a parallelogram: opposite sides are parallel, adjacent angles are supplementary, and the two diagonals bisect one another.
Not every parallelogram is a rhombus, though any parallelogram with perpendicular diagonals (the second property) is a rhombus. In general, any quadrilateral with perpendicular diagonals, one of which is a line of symmetry, is a kite. Every rhombus is a kite, and any quadrilateral that is both a kite and parallelogram is a rhombus.
Parallelogram:
* Opposite sides of a parallelogram are equal in length. * Opposite angles of a parallelogram are equal in measure. * Adjacent angles are supplementary (add up to 180 degrees). * Opposite sides of a parallelogram are parallel (by definition) and so will never intersect. * Any line through the midpoint of a parallelogram bisects the area.[1] * The diagonals of a parallelogram bisect each other. * A parallelogram has rotational symmetry of order 2 (through 180°). If it also has two lines of reflectional symmetry then it must be a rhombus or a rectangle. * The perimeter of a parallelogram is 2(a + b) where a and b are the lengths of adjacent sides. * The sum of the squares of the sides equals the sum of the squares of the diagonals.[2]
Well, with all of these flowery definitions, the same thing keeps appearing. Class and sub-class. The way I understand it is this:
The square is a special type of rectangle, a sub-class of rectangle (equal angles) and/or rhombus (equal sides), which are both parallelogram's. Therefore the square EXTENDS (or has special properties of) both a rectangle and a rhombus, which both EXTEND a parallelogram.
But, a rectangle IS NOT a square. Neither is a Rhombus. I would deduce that a sub-classification is specialized, exhibiting some of the same properties as the parent class, along with some special properties of its own. The parent class does not have the sub-classes special properties.
Therefore, a rectangle does NOT EXTEND a square. It is a 'one-way street'.
One interesting point is that a square extends both a Rhombus and a Rectangle. From a purely Java perspective, a class can only have one parent, and a Java class can only extend one other class.
You mentioned 'sub-class.' The term 'subclass' denotes a Java class that extends another Java class. There is another term that is more general, and it is the idea of a 'sub-type.' Sub classes are sub-types, but not all subtypes are sub-classes. :) Subtypes are 'implemented' in a slightly different way, with stress on the word 'implemented.'
About the parent...A class can have only one parent, but that parent can have a parent. The long list of classes that make up a hierarchy is known as the ancestry. So, a class can have only one parent, but it may have many ancestors, and the is-a relationship is valid anywhere up the chain. So a Square is-a Rectangle which is-a Shape which is-a(n) Object.
4 comments:
Sometimes the term 'constructor' becomes intimidating. Really, it's just the special method that a class has that people call in order to 'create' or 'construct' an instance of a class.
So, if you have a class named Rectangle, it has a constructor with the name Rectangle, and if that constructor takes two 'int' arguments, anyone could create an instance of that Rectangle with the following code:
Rectangle r1 = new Rectangle(2,3);
We also talked about 'specialization' and 'abstraction.' Really, we talked about the extends keyword, and it indicates that a 'subclass' that extends a 'parent class' is a 'special type of that parent class.'
In terms of our example, we said:
public class Square extends Rectangle {}
The meaning of that is that a Square is a 'special type' of Rectangle. Is the opposite true though? Is a Rectangle a special type of Square? Talk amongst yourselves...
To argue the question, let's look at Wiki's of definitions of a square and rectangle (it's been a long, long time since highschool . . . . lol).
Square:
A square is both a rhombus (equal sides) and a rectangle (equal angles) and therefore has all the properties of both these shapes, namely:
* The diagonals of a square bisect each other and meet at 90 degrees.
* The diagonals of a square bisect its angles.
* The diagonals of a square are perpendicular.
* Opposite sides of a square are both parallel and equal.
* All four angles of a square are equal. (Each is 360/4 = 90 degrees, so every angle of a square is a right angle.)
* The diagonals of a square are equal.
Rectangle:
In Euclidean plane geometry, a rectangle is any quadrilateral with four right angles. The term "oblong" is occasionally used to refer to a non-square rectangle.
Rhombus:
Every rhombus has two diagonals connecting opposite pairs of vertices and two pairs of parallel sides. Using congruent triangles, one can prove that the rhombus is symmetric across each of these diagonals. It follows that any rhombus has the following properties:
1. Opposite angles of a rhombus have equal measure.
2. The two diagonals of a rhombus are perpendicular; that is, a rhombus is an orthodiagonal quadrilateral.
3. Its diagonals bisect opposite angles.
The first property implies that every rhombus is a parallelogram. A rhombus therefore has all of the properties of a parallelogram: opposite sides are parallel, adjacent angles are supplementary, and the two diagonals bisect one another.
Not every parallelogram is a rhombus, though any parallelogram with perpendicular diagonals (the second property) is a rhombus. In general, any quadrilateral with perpendicular diagonals, one of which is a line of symmetry, is a kite. Every rhombus is a kite, and any quadrilateral that is both a kite and parallelogram is a rhombus.
Parallelogram:
* Opposite sides of a parallelogram are equal in length.
* Opposite angles of a parallelogram are equal in measure.
* Adjacent angles are supplementary (add up to 180 degrees).
* Opposite sides of a parallelogram are parallel (by definition) and so will never intersect.
* Any line through the midpoint of a parallelogram bisects the area.[1]
* The diagonals of a parallelogram bisect each other.
* A parallelogram has rotational symmetry of order 2 (through 180°). If it also has two lines of reflectional symmetry then it must be a rhombus or a rectangle.
* The perimeter of a parallelogram is 2(a + b) where a and b are the lengths of adjacent sides.
* The sum of the squares of the sides equals the sum of the squares of the diagonals.[2]
Well, with all of these flowery definitions, the same thing keeps appearing. Class and sub-class. The way I understand it is this:
The square is a special type of rectangle, a sub-class of rectangle (equal angles) and/or rhombus (equal sides), which are both parallelogram's. Therefore the square EXTENDS (or has special properties of) both a rectangle and a rhombus, which both EXTEND a parallelogram.
But, a rectangle IS NOT a square. Neither is a Rhombus. I would deduce that a sub-classification is specialized, exhibiting some of the same properties as the parent class, along with some special properties of its own. The parent class does not have the sub-classes special properties.
Therefore, a rectangle does NOT EXTEND a square. It is a 'one-way street'.
One interesting point is that a square extends both a Rhombus and a Rectangle. From a purely Java perspective, a class can only have one parent, and a Java class can only extend one other class.
You mentioned 'sub-class.' The term 'subclass' denotes a Java class that extends another Java class. There is another term that is more general, and it is the idea of a 'sub-type.' Sub classes are sub-types, but not all subtypes are sub-classes. :) Subtypes are 'implemented' in a slightly different way, with stress on the word 'implemented.'
About the parent...A class can have only one parent, but that parent can have a parent. The long list of classes that make up a hierarchy is known as the ancestry. So, a class can have only one parent, but it may have many ancestors, and the is-a relationship is valid anywhere up the chain. So a Square is-a Rectangle which is-a Shape which is-a(n) Object.
Post a Comment