![]() you have paint(), and getArea(), let's add getCircumference()): with if-else solution you only have to alter just one file - the file which will contain the new method. So let's compare the amount of changes:Īdding a new method (eg. ![]() There's so called open-closed principle, which basically means, that when you add a new feature to an existing program, the less changes you make in existing code the better (because every change requires some work, and may introduce bugs). (or switch, or Visitor), and between polymorphism is modularity. Other examples of compile time polymorphism are constructor overloading and method hiding.ĩ.The main difference between if.else. How to achieve or implement static polymorphism in Java?Īns: Static polymorphism can be achieved by method overloading. In static polymorphism, the behavior of method is decided at compile-time based on the parameters or arguments of method.Ĩ. What is Compile time polymorphism (Static polymorphism)?Īns: A polymorphism where object binding with methods happens at compile time is called static polymorphism or compile-time polymorphism. Without polymorphism, it is not possible for a child class to execute its own behavior.ħ. On the other hand, polymorphism enables child class to redefine already defined behavior inside parent class. Inheritance helps in code reusability in child class by inheriting behavior from parent class. On the other hand, polymorphism takes the advantage of that relationship to make the program more dynamic.ī. Inheritance represents the parent-child relationship between two classes. What are the differences between Polymorphism and Inheritance in Java?Īns: The differences between polymorphism and inheritance in java are as follows:Ī. The main benefit of using polymorphism is when we can provide implementation to an abstract base class or an interface.Ħ. Using polymorphism, we can achieve flexibility in our code because we can perform various operations by using methods with the same names according to requirements.ī. What are the advantages of Polymorphism?Īns: There are the following advantages of polymorphism in java:Ī. How is Inheritance useful to achieve Polymorphism in Java?Īns: Inheritance represents the parent-child relationship between two classes and polymorphism take the advantage of that relationship to add dynamic behavior in the code (or to make the program more dynamic).ĥ. The subclasses can have their own unique implementation for certain features and can also share some of the functionality through inheritance.Ĥ. What are different ways to achieve or implement polymorphism in Java?Īns: Polymorphism in Java can be primarily achieved by subclassing or by implementing an interface.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |