We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
1. What are the advantages of Polymorphism?
- Flexibility and Scalability: The capacity of polymorphism to perform multiple
actions depending on the object on which it acts gives methods greater flexibility and scalability in the code. - Code Reusability: This allows methods in other classes that perform comparable tasks but with different inputs to be reused. - Maintainability: Allows the same interface to be used for several underlying forms (data types), which reduces complexity. This facilitates code maintenance and modification. - Interchangeability and extensibility: In big projects where software behaviour needs to be changed quickly and effectively, objects of various classes can be considered as objects of a shared superclass.
2. How is Inheritance useful to achieve Polymorphism in Java?
One way to do polymorphism is through inheritance. In Java, this is how it operates: - A class can inherit methods and attributes from another class, referred to as the superclass, through inheritance. The subclass can enhance or override the functionality of its superclass thanks to this connection. - Overriding the Method: This is a characteristic of inheritance when a method in the child class has the same name, parameters, and return type as a method in the parent class. Polymorphism is demonstrated when an overridden method is invoked from an object of the child class; this calls the method's child class version. - Interface Implementation: Another method of achieving polymorphism in Java is to allow classes to implement interfaces. All of the methods specified in an interface must have an implementation provided by the classes that implement it, guaranteeing a shared interface but different implementations. 3. What are the differences between Polymorphism and Inheritance in Java? Conceptual Distinction: - A new class is created by inheriting properties from an existing class. Its main objective is to create a new class by using an existing one. - The ability of several objects to react differently to the same method call is known as polymorphism. It emphasises on utilising techniques and interfaces in various contexts. Use: - Inheritance creates a connection between the base class and the derived class, which inherits methods and attributes. - There are several methods to carry out the same activity thanks to polymorphism. Conceptual Distinction: - A new class is created by inheriting properties from an existing class. Its main objective is to create a new class by using an existing one. - The ability of several objects to react differently to the same method call is known as polymorphism. It emphasises on utilising techniques and interfaces in various contexts. Application of the Method: - Inheritance creates a connection between the base class and the derived class, which inherits methods and attributes. - There are several methods to carry out the same activity thanks to polymorphism.