• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism without inheritance in java  RSS feed

 
Amy Jose
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can any one please explain how to implement polymorphism without inheritance in java.
It would be useful if it is explained starting with what is polymorphism, types of polymorphism and polymosrphism with inheritance and without inheritance.

Thanks
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Amy!
Polymorphism without Inheritance sounds strange.
Would be helpful if you give some background behind your question.
It sounds like a typical twisted interview question
 
BalaMurali dhar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism : Polymorphism is a charactertisic of being able to assign a different behaviour or value in a sub class which was something to declare in a parent class.
Types of Polymorphism : There are two types of polymorphism. One is compile time polymorphism and another is run time polymorphism.
Compile Time Polymorphism : It is method overloading.
Method Overloading : When two or more methods with same name in the same class with different arguments. This is called method overloading.
Run Time Polymorphism : Run Time Polymorphism is done by using inheritance & interface. Run Time Polymorphism is a process in which a call to an overridden method is resolve at run time rather than at compile time. In this process an overridden method is called through the reference variable of a superclass

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,you can achieve polymorphism without inheritance through interface implementation.
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:Yes,you can achieve polymorphism without inheritance through interface implementation.

Implementing Interfaces is Inheritance.

Example:
Car implements Vehicle
hence Car is a Vehicle. Is-A relationships are achieved using inheritance. Ref. Chapter 1, Head First Design Patterns
 
ak khatri
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can achieve polymorphism without inheritance using composition.

Composition is an oop topic, when we create an object of a class in an other class we called it composition.

http://www.artima.com/objectsandjava/webuscript/CompoInherit1.html

visit this link.
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ak khatri wrote:you can achieve polymorphism without inheritance using composition.

Composition is an oop topic, when we create an object of a class in an other class we called it composition.

http://www.artima.com/objectsandjava/webuscript/CompoInherit1.html

visit this link.


I disagree, below is what your link says about Composition -

The relationship modeled by composition is often referred to as the "has-a" relationship. In this case a CoffeeCup has Coffee. As you can see from this example, the has-a relationship doesn't mean that the containing object must have a constituent object at all times, but that the containing object may have a constituent object at some time. Therefore the CoffeeCup may at some time contain Coffee, but it need not contain Coffee all the time. (When a CoffeeCup object doesn't contain Coffee, its innerCoffee field is null.) In addition, note that the object contained can change throughout the course of the containing object's life.

Which is not Polymorphism.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!