9/23/2023 0 Comments Polymorphism java definition![]() ![]() This code will display "Yes, rect is an an instance of Rectangle." ("No, rect is not an instance of Rectangle.") ("Yes, rect is an instance of Rectangle.") Otherwise, the expression returns false.įollowing code segment illustrates how the operator instanceof works : Rectangle rect = new Rectangle() This is the form of a boolean expression that will return true if the object referenced by The instanceof operator is an boolean operator that tests whether an object belongs to a given class. Rect = (Rectangle)shape // Downcasting The instanceof Operator Downcasting occurs when a type cast is performed from a super class to a sub class. Or we could do something equivalent, such as Shape shape = new Rectangle() // Upcasting This is known as upcasting : Shape shape ![]() ("Area of triangle : " + tri.getArea()) Īs illustrated above, a reference variable of a superclass type can point to an object of its ("Area of rectangle : " + rect.getArea()) assign subclass reference to subclass variable * This program demonstrates polymorphic behavior. Public void setValues(double height, double width) Private double width //To hold width or base * A class Shape that holds width and height TheyĬan refer to objects of their own class or to objects of the subclasses inherited from their class. Variables are polymorphic reference variables. So we can summarize this with the statement that Super class reference So all statements are same for triangle object. Similarly Triangle is also sub class of Shape class. As shown in following statement : double a = rect.getArea() // legal So in order to access getArea method of Rectangle class, we will use Rectangle type reference variable. double a = shape.getArea() //illegalĪlthough a shape variable can reference object of class that extends Shape but a shape variable can be used to call only those methods defined inside Shape. Now consider the statement, we wants to call getArea method using shape reference variable. tValues(4,5) // Set values in rectangle object. Shape = rect // shape point to the object rect.Īfter the above statement executes, the object is treated as an object of the class Shape. 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |