• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism  RSS feed

 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can we achieve Polymorphism in java

1)overriding
2)overloading

or both 1 and 2
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism In Java
 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still i don't have clear idea is both overloading and overriding support Polymorphism??

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aruna sameera wrote:still i don't have clear idea is both overloading and overriding support Polymorphism??

Polymorphism - Many Forms.

So one method exhibit and behave in many forms.

In both(Overloading and Overridng), same method name will show different results as per the object who calls it(method overriding) and as per method parameters passed(method overloading).

Can you make any example from it?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism is derived from the chains of inheritance in Java. Inheritance shows a relationship between two or more classes in which the parent class (also called the superclass) passes attributes and methods to the child class (also called the subclass).

Example:
You have three classes: Insect, Termite, and Bee;
In inheritance the more broad term is always the Parent class, so in this case the Parent class is Insect; because insect can apply to any number of different types of insect.

The two child classes (Termite and Bee) inherit attributes and methods from the Parent class (Insect).









Output of testInsects:
The bee flys away
The termite crawls away

Name: Bee hasWings: true
Name: Termite hasWings: false


Hope this helps.

Hunter M.

(Please note I'm not sure if Termites do or do not have wings but for this example, let's just assume they dont )
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!