Suppose that there are four classes in a hierarchy. The superclass is Shape, and
ID: 3688677 • Letter: S
Question
Suppose that there are four classes in a hierarchy. The superclass is Shape, and the subclasses of Shape are Recangle, Circle, and Triangle. Each has the method calculateArea(). C) Are all the methods calculateArea() in these four classes the same? note that a subclass inherits methods from its super class. D) if not, is an object of a superclass also an object of a superclass? F) why do we need abstract classes? e) Does Circle i nherit all attributes and methods in TwoDImensionalShape? Do we have to implement area() for circle? can we just imherit area() from TwoDimensionalShape and then from Shape and use it?
Explanation / Answer
C)
super class have calculateArea() method .but as subclasses are extending the super class Shape they will also get the calculateArea() method.but different subclasses will have different implementation for the calculateArea() method inorder to calculate the area of Rectangle ,Circle,Traingle.I.e we have to override the calculateArea() in subclasses based on the dimensions required to calculate the area.
So the method calculateArea() which are present in all the four classes are not same.
_________________________________________________________________________________________
F)
We need abstract classes if we want to use the same method names in its subclasses and need to provide different implementations to the method body in its subclasses.
Abstract classes enforcing us to provide same method names in all its subclasses
__________________________________________________________________________________________
E)
If TwoDImensionalShape is the super of circle it can inherit the attributes and methods from the super class TwoDImensionalShape.Depending upon dimensions we can override the area() method in the Circle class.
We cant just inherit the area() method from its super class TwoDImensionalShape.
________________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.