Java / Android Developer, Trainer, Writer, Speaker

Wednesday, February 4, 2015

On 4:39 AM by Unknown in ,    9 comments

Like the article?

Polymorphism 

We are going to create four Java class in our Polymorphism class. Those are:

1. human.java class
2. women.java class
3. men.java class
4. main.java class

Polymorphism Classes

The codes are given below.



1. human.java

package polymorphism;

/* 

 What is polymorphism ?
 Answer: Coming out in different form when needed is called polymorphism.
 Polymorphism is a OOP ( Object Oriented Programming concept ). In more words,
 Java's polymorphism  means the ability of Java's ability utilize objects 
 depending on their particular data type and class.
 
 This is my super class. 
 
 */
public class human {

 public void goodPublic(){
  System.out.println(" In java human class "
    + "im printing good human method to understand polymorphism");
 }
}





2. women.java

package polymorphism;

// This is my subclass
public class women extends human{
 public void goodPublic(){
  System.out.println("This is a method printed output from WOmen class");
  
 }
}




3. men.java

package polymorphism;

// this is my subclass

public class men extends human {
 public void goodPublic(){
  System.out.println("This is a method printed output from men class");
  
 }
}




4. main.java

package polymorphism;

public class main {

 public static void main(String[] args) {
  
  /*
   Creating object of human class using inheritance OOP concept!
   We can create object as below using the inheritance as those sub class
   inherits human super class. 
  
   In Below,
   meHuman,goodMen,goodWomen are reference variable.
   human is working as data type.
   */
  
  human meHuman = new human(); 
  human goodMen = new men();
  human goodWomen = new women();
 
  
  /* 
   
    Now I'm going to create a Polymorphism array below of the super class
    and put the subclass objects there. 
    
   
   */
  
  human[] polymorphicArray = new human[3];
  
  polymorphicArray[1] = new women();
  polymorphicArray[2] = new men();
  polymorphicArray[3] = new human();
    
  // Printing those methods from subclass
  
  polymorphicArray[1].goodPublic();
  polymorphicArray[2].goodPublic();
  
  
  /* 
   
    Here, this array is a array of polymorphism. This array has the ability
    to utilize objects differently depending on data type or class.
   
   */
    
 }
/* 
  In final words, if u call a animal Sound super class and cat, dog subclass.
  no matter what the animal is working with the animal sound
   will return you the real animal sound.
  so, animal sound method is working as cat sound once and dog sound once. 
  That's the different form! That's called polymorphism.
 */
}

  

Overriding: In inherited subclass, if a method uses same name as the method declared in super class but the different method body is called as overriding.


Overloading: In inherited subclass, if a method uses same name as the method declared in super class but the different method body and different arguments is called as Overloading.



Read More: What are actually taught in computer science and Engineering ( CSE ) ?

9 comments:

  1. Thank you for sharing such a nice and interesting blog with us. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
    Best Dental Clinic In Vellore

    ReplyDelete