![]() In other words, we can use a Shape type reference variable to reference a Rectangle object : Shape shape Therefore an object of the Rectangle class is not just a Rectangle object. In above figure the Shape class is used as the superclass for the Rectangle class. Therefore, weĬan use the rect variable to reference a Rectangle object, as shown in the following statement: rect = new Rectangle() This statement tells us that the rect variables data type is Rectangle. Look at the following statement that declares a reference variable named rect: Rectangle rect Reference variable of a superclass type can point to an object of its subclass. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature.Ĭonsider the following example, Shape is super class for Rectangle and Triangle class. ![]() ![]() One of the key features of inheritance is that a
0 Comments
Leave a Reply. |