230k views
3 votes
Create a segment of code that initializes a public class Fish. Let the class contain a String typeOfFish, and an integer friendliness. Do not set values to these variables yet. These are instance variables and will be set inside the class constructors.

1 Answer

2 votes

Answer:

The code to the given question as follows:

Code:

public class Fish //defining class

{

private String typeOfFish; //defining variable

private int friendliness; //defining variable

Fish(String typeOfFish, int friendliness) //parameterized constructor.

{

super(); //calling

this.typeOfFish = typeOfFish; //holds value in variable.

this.friendliness = friendliness; //holds value in variable.

}

}

Step-by-step explanation:

  • In the above code, a class "Fish" is defined that contains two private variables that are "typeOfFish and friendliness" in which typeOfFish is a string type variable and friendliness is an integer type variable.
  • In this class, a parameterized constructor is defined, which includes these variables as a parameter and inside this constructor, the super keyword and this keyword is used.
  • Super keyword and this keyword both is a reference variable, but the super keyword is used to call the above class or parents class object and this keyword is used as a reference to holding the current object.
User Sebamed
by
5.9k points