Define the following with an example each. (a) Inheritance (b) Polymorphism
Answer:
(a) Inheritance is a property by virtue of which one class acquires some features from another class. It promotes reusability.
As an example of inheritance, we can consider the case of vehicles. All vehicles have some common features like they can move on the road and transport people and goods from one place to another. These vehicles differ from each other in certain aspects like whether it transports passengers or goods, how many passengers it can accommodate at a time, whether it is a two-wheeler or four-wheeler, etc. So, we have different types of vehicles like Cars, Bikes, Scooters, Auto rickshaw, Buses, Trucks, etc. Using inheritance, we can make vehicles the base class with the different types of vehicles being the derived class as shown below:
(b) Polymorphism is the process of using a function for carrying multiple operations. During this process, an object may include a function for multiple operations.
As an example, if we ask different animals to speak, they respond in their own way. Dog will bark, duck will quack, cat will say meow and so on. So, the same action of speaking is performed in different ways by different animals. This is the concept of Polymorphism.
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.