Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Constructor problem

Constructor problem

Scheduled Pinned Locked Moved Java
helpdatabase
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john_meee
    wrote on last edited by
    #1

    I have a problem with constructors ... I never understood how they work. At Caine c=new Caine(); Animal c1=new Caine(); Pisica p=new Pisica(); Error: Constructor Caine in Caine class cannot be applied to given class

    package test2;
    public class Test2 {

     public static void main(String\[\] args) {
         ListaAnimale lista= new ListaAnimale();
         Caine c=new Caine();   
         Animal c1=new Caine();        
         Pisica p=new Pisica();
         lista.adauga(c);
         lista.adauga(p);
         c.faceZgomot();
     }
    

    }
    class ListaAnimale{
    private Animal [] animale = new Animal[5];
    private int index=0;
    public void adauga(Animal a){
    if(index

    T V T 3 Replies Last reply
    0
    • J john_meee

      I have a problem with constructors ... I never understood how they work. At Caine c=new Caine(); Animal c1=new Caine(); Pisica p=new Pisica(); Error: Constructor Caine in Caine class cannot be applied to given class

      package test2;
      public class Test2 {

       public static void main(String\[\] args) {
           ListaAnimale lista= new ListaAnimale();
           Caine c=new Caine();   
           Animal c1=new Caine();        
           Pisica p=new Pisica();
           lista.adauga(c);
           lista.adauga(p);
           c.faceZgomot();
       }
      

      }
      class ListaAnimale{
      private Animal [] animale = new Animal[5];
      private int index=0;
      public void adauga(Animal a){
      if(index

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #2

      Caine c=new Caine(); // That constructor does not exist
      Animal c1=new Caine(); // That constructor does not exist
      Pisica p=new Pisica();// That constructor does not exist

      Your constructors are demanding some values:

      public Caine(String n, int a){ // String and Int is required to initalize
      super(n,a);
      }

      so your main would initalize those as following:

      Caine c=new Caine("String", 0);
      Animal c1=new Caine("anotherString", 1);
      Pisica p=new Pisica("oneMoreString", 2);

      Constructors are not that complicated. it's just some OOP as in any other method that you write. http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html[^]

      regards Torsten I never finish anyth...

      1 Reply Last reply
      0
      • J john_meee

        I have a problem with constructors ... I never understood how they work. At Caine c=new Caine(); Animal c1=new Caine(); Pisica p=new Pisica(); Error: Constructor Caine in Caine class cannot be applied to given class

        package test2;
        public class Test2 {

         public static void main(String\[\] args) {
             ListaAnimale lista= new ListaAnimale();
             Caine c=new Caine();   
             Animal c1=new Caine();        
             Pisica p=new Pisica();
             lista.adauga(c);
             lista.adauga(p);
             c.faceZgomot();
         }
        

        }
        class ListaAnimale{
        private Animal [] animale = new Animal[5];
        private int index=0;
        public void adauga(Animal a){
        if(index

        V Offline
        V Offline
        vipul574
        wrote on last edited by
        #3

        At Caine c=new Caine(); Animal c1=new Caine(); Pisica p=new Pisica(); These 3 statements wont work. As you can see, your program does not have no-arg constructor. when you dont write any constructor, then compiler will include a default no-arg constructor. If you are explicitly writing a constructor then default constructor wont be added. You have to write your own default no-arg constructor. Then the above lines will run successully.

        Regards Vipul

        1 Reply Last reply
        0
        • J john_meee

          I have a problem with constructors ... I never understood how they work. At Caine c=new Caine(); Animal c1=new Caine(); Pisica p=new Pisica(); Error: Constructor Caine in Caine class cannot be applied to given class

          package test2;
          public class Test2 {

           public static void main(String\[\] args) {
               ListaAnimale lista= new ListaAnimale();
               Caine c=new Caine();   
               Animal c1=new Caine();        
               Pisica p=new Pisica();
               lista.adauga(c);
               lista.adauga(p);
               c.faceZgomot();
           }
          

          }
          class ListaAnimale{
          private Animal [] animale = new Animal[5];
          private int index=0;
          public void adauga(Animal a){
          if(index

          T Offline
          T Offline
          Tushar Guru
          wrote on last edited by
          #4

          Hi, you can learn "All About Constructors in Java" from here. It is the best and simple one.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups