Results 1 to 6 of 6

Thread: need help

  1. #1
    Nawaf is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default need help

    Hey guys.

    i want to loop through each of my objects and call the method for each one. ( polymorphic array )

    Error: Constructor Dog in class Dog cannot applied for gives Types
    Required String,String
    Found: no arguments
    Reason actual and formal argument lists differ in length



    Java Code:
    public class animal {
     private String m_type="";
     private String m_name=""; 
     
     public static void main (String[] args)
     {
         
    animal array[]=new animal[3];
         array[0]=new fish();
         array[1]=new bird();
         array[2]=new dog();
         
         for ( int x=0 ; x<2 ; x++)
         {
             array[x].greet();
         }
        
     }
     
     
               
           }
    Last edited by Nawaf; 11-22-2014 at 03:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: need help

    Error: Constructor Dog in class Dog cannot applied for gives Types
    Required String,String
    Found: no arguments
    Reason actual and formal argument lists differ in length
    Either add a no arg constructor to the class
    or pass the proper args to the constructor.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Nawaf is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Re: need help

    How can i add a no arg constructor to the class? sorry im new to this

  4. #4
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: need help

    For example:

    Java Code:
    public ClassName() {
    
    }
    Literally a constructor that doesn't take in any parameters. If your constructor does anything else other than use the parameters it's given then just leave it empty. You can have more than one constructor in a class with different parameters to the other one.

    EDIT: E.g.:

    Java Code:
    public class ClassA {
    	int a;
    	
    	public ClassA(int a) {
    	this.a = a;
    	}
    	
    	public ClassA() {
    	
    	}
    }
    Last edited by augustas656; 11-22-2014 at 03:46 PM.
    Regards
    augustas656

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: need help

    And you're only going to call two methods. Get in the habit of using the array length attribute. And by convention, class names should start with uppercase letters. See my signature for more Java naming conventions.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Re: need help

    All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program.
    ----------
    Example
    ---------
    class A{
    A(){
    System.out.println("A con");
    }
    }
    class Test{
    public static void main(String[] args){
    A a=new A();

    }
    }
    Last edited by debiprasadboxi; 11-22-2014 at 07:09 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •