AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Polymorphism java quiz8/5/2023 ![]() ![]() Q5 How you can achieve static polymorphism in Java? Static Polymorphism is a form of polymorphism that decides which method to execute at compile time. Q4 What is static polymorphism (compile time polymorphism) in Java? The child class inherits behavior from the parent class whereas Polymorphism enables the child class to redefine already defined behavior inside the parent class. One of the advantages of Inheritance is code reusability in child class. Inheritance shows the parent-child relationship between two classes whereas Polymorphism takes the advantage of that relationship to make the program more dynamic.Ģ. The major differences between Polymorphism and Inheritance in Java are listed below:ġ. Q3 What is the difference between Polymorphism and Inheritance in Java? Dynamic Polymorphism (Runtime Polymorphism) ![]() Static Polymorphism (Compile time Polymorphism)Ģ. There are two types of Polymorphism in Java:ġ. Q2 What are the different types of Polymorphism in Java? Polymorphism Interview Questions and Answers Q1 What is Polymorphism in Java? Read Also: Polymorphism quiz (multiple choice questions and answers) ![]() Using instanceof can be considered a code smell, as you would have to update all your conditionals whenever you add a new class.In this post, I will be sharing the most frequently asked polymorphism interview questions and answers in Java for both fresher and experienced professionals. We don't care what the implementation class does, only what is defined by the interface (or abstract parent class).Įdit: Regarding the example in the question, It is generally recommended to avoid that kind of code pattern by encapsulating behaviour where possible. In this code, we are directly calling Shape.draw() on p1 and p2. In an ideal world, we don't want to have to worry about what specific type of object we are dealing with, only the interface of a more general type that covers all usage scenarios in its interface. ![]() For example, if Shape defines an abstract draw() method, then Triangles, Squares and anything else that extends Shape will also have the same method.Ī simple definition for polymorphism is "treating different types as if they are the same", i.e. The idea is that you shouldn't have to care what kind of shape you are dealing with. with Visitor pattern or just making an enum with a separate case for each branch. branches, but in lots of places, you should consider encapsulating this if.else. branches, you should rather use polymorphism, when you expect few such branches, just stay with if.else.Īdditionally: when you expect few if.else. Or in other words: when you expect to have many if.else. If you expect that many new sublasses will be added later, use polymorphism if you expect that many new methods will be added later, use if.else., and in the class put only the most "basic" methods like accessors. or polymorphism: it depends on modularity. With polymorphism, you have to alter all your implementations of Shape class.Īdding a new kind of Shape (you have Square, Circle - let's add Triangle): with if-else solution you have to review all existing classes with if-else and add a new if branch for Triangle with polymporphism all you have is to add a new class and implement all required methods in it. 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. ![]()
0 Comments
Read More
Leave a Reply. |