networksqert.blogg.se

Polymorphism java example shape of objects
Polymorphism java example shape of objects









polymorphism java example shape of objects

Variable Type and Object Type Below I frequently refer to two terms i.e.

Polymorphism java example shape of objects software#

This approach of designing software makes it more extensible and also reduces to code size substantially. Polymorphism help to program in "general" rather writing program with "specific" classes. in method parameters or instance variables), we prefer to use generic classes rather specific, as long as they can serve the purpose. Generalization is also called Abstraction, because more we move at top of hierarchy, our entities/concept become more abstract. So, if we view classes from top to bottom of the hierarchy, we move from generalization to specialization (or from generic to specific entity). In same way, Pet is more specific compared to Animal, Animal is specific compared to LivingBeing. So we say, Cat is more specific entity compared to Pet (or Pet is generic and Cat is specific). If we look at classes defined in lower side of the hierarchy, these are more specific entities.

polymorphism java example shape of objects

You see, classes are upper side of inheritance hierarchy refers to more generic concepts as compared to classes in bottom side of hierarchy, hence the term generalization. Generalization and Specialization Assume classes are defined in a way such that, Cat extends Pet, Pet extends Animal and so on. Assume following classes exist in an inheritance hierarchy:

polymorphism java example shape of objects

Before I explain polymorphism, you must understand what is generalization and specialization in context of object oriented design.











Polymorphism java example shape of objects