public class Solution {
public static void main(String[] args) {
Cat cat1 = new Cat();
Cat cat2 = new Cat();
Cat Cat3 = new Cat();
cat1.Cat("Kitty",6,12,13);
cat2.Cat("Mary",7,13,14);
cat3.Cat("Sany",8,14,15);
}
public static class Cat {
private String name;
private int age;
private int weight;
private int strength;
public Cat(String name, int age, int weight, int strength) {
this.name = name;
this.age = age;
this.weight = weight;
this.strength = strength;
}
}
}
where is the problem?how to solve?
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Arshdeep
25 May 2020, 20:18
Cat cat1 = new Cat("Kitty",5,8,10);
Cat cat2 = new Cat("Lucy",6,8,11);
Cat cat3 = new Cat("Mini",8,8,12);
just define the three cat objects in the main function like this as the class cat is defined in parametrized constructor!
0
KevinTing
19 February 2019, 03:00
public Cat is the Cat Class constructor with 4 parameters
+1
Vatsal Patel
7 February 2019, 22:58
There is no default constructor so you cannot do: Cat cat1 = new Cat();
You need to do: Cat cat1 = new Cat("Cat1", 2, 10,15);
+1
Anita Molnár
31 October 2019, 07:01
What do you mean "default"? Can you explain this please, because I don't get this. I used to create object like this before with no problem and I don't understand why isn't work it here.
0
Abdul-Hamed Mohammed
16 February 2020, 08:59
public Cat(String name, int age, int weight, int strength) {
this.name = name;
this.age = age;
this.weight = weight;
this.strength = strength;
The above is a parameterised constructor in a Cat class. So, once we define a constructor for a Cat class which is parameterised constructor then the default constuctor goes away.
If you want one then we have to define it again like below.
Default construct = public Cat(){
}.
When we do not explicitly define a constructor for a class, then java creates a default constructor for the class. It is essentially a non-parameterized constructor, i.e. it doesn't accept any arguments. The default constructor's job is to call the superclass constructor and initialize all instance variables.
0
Pavlo Plynko Java Developer at CodeGym
18 August 2018, 21:12useful
+4