![]() area() method defined in the Rectangle class area() method defined in the Triangle class So, the call to the area() method must be different under different circumstances. This means that every subclass must provide the implementation of that method. Suppose there is an abstract method called area() declared in the abstract Shape class. We always can create a reference to the topmost class object in the inheritance hierarchy, such as, Shape s1 Ī superclass reference variable can refer to a subclass object, but a subclass reference variable cannot refer to a superclass object (unless explicitly casted, which we shall later down the line). A polymorphic reference can refer to different objects at different points of time. The variable type and the object it refers to must be compatible, but their types need not be the same. The triangle is a reference to the Triangle class object it can be equally used to refer to the object of IsocelesTriangle. For example,įigure 1: A shape hierarchy chart Triangle triangle This, however, is not always the case with polymorphism. ![]() Typically, the type of the reference variable exactly matches with the class object that it refers to. So, polymorphism in Java is in a way to enable us to create a class in general, which becomes more specific as we extend it through sub-classification. However, the Rectangle has the option to use the generic implementation of the area() method defined by its superclass otherwise, we can modify it to meet our specific needs. Now, if we extend this class to create a Rectangle class (the area formula is quite different from a triangle had we created a class called Triangle), we need to write only the Rectangle class and part of the code that instantiates the Rectangle object. Other derived implementation may be used as it is implemented by the superclass without making any changes.įor example, a Shape class contains a method called area(). As per the norms of inheritance, a new class acquires the property and methods of the superclass and is open to override only those methods that it is interested in modifying. The new class becomes part of the classification, like a Lego attached to a construction in such a manner that the construction would not crumble even if we detach one. That means we can assign new classes with almost no modification of the existing code, provided the class is part of the inheritance hierarchy. ![]() Polymorphism in Java Leverages Extensibility This article is a attempt to explore the concept with a focus on Java with appropriate illustrations and examples. The term “polymorphic” means “having multiple forms.” Polymorphism in Java simplifies programming by providing a single interface overlaid with multiple meanings as it goes through the rigor of subclassing. Polymorphism in Java is closely associated with the principle of inheritance. We may make money when you click on links to our partners. content and product recommendations are editorially independent. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |