Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An object is a member or an \"instance\" of a class. An object has a state in wh

ID: 3823692 • Letter: A

Question

An object is a member or an "instance" of a class. An object has a state in which all its properties have values that you either explicitly define or that are defined by default settings. This subtle conceptual difference between classes and objects shows why there is a tendency to want to use them interchangeably. An example will help clarify what we've said. Suppose we have a class called 'Animal'. All Animals have bodies - and brains these could be the attributes of our fictional Animal class. We can also add some methods that would be common to all Animals - like "movement", because all animals can move (maybe you can think of a better example for methods, but hopefully you get the point). So, the idea you want to enforce in your own mind is that this very general template' of an Animal does not change - it's simply just some lines of code that define the Animal class^1. Objects have a lifespan but classes do not And, as our Animal example clearly shows, every object has a lifespan associated with - it a cat or zebra cannot live forever. And, the properties of those objects can change as well while they 'live'; if we have a 'size' variable defined in the class that would of course change as the cat object grows bigger.^ii Object versus class summary So, we can say that whereas a class is a general concept (like an Animal), an object is a very specific embodiment of that class, with a limited lifespan (like a lion, cat, or a zebra). Another way of thinking about the difference between a class and an object is that a class provides a template for something more specific that the programmer must define, which he/she will do when creating an object of that class. Your own example of a class, and construct 2 different objects from the class you designed.

Explanation / Answer

Hi,

Please see below the classes. Please comment for any queries/feedbacks.

Thanks

Animal.java

/**
* Class Animal
*
*
*/
public class Animal {

   /*Instance variables */
   private String name;
   private int lifeSpan;
   private String size;
   private int noOfLegs;
  
  
   //No-arg constructor
   public Animal() {
       this.name="";
       this.lifeSpan = 0;
       this.size ="";
       this.noOfLegs =0;
      
   }

   //Parameterised Costructor
   public Animal(String name,int lifeSpan, String size, int noOfLegs) {
       super();
       this.name = name;
       this.lifeSpan = lifeSpan;
       this.size = size;
       this.noOfLegs = noOfLegs;
   }

  
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   //Getters and Setters
   public int getLifeSpan() {
       return lifeSpan;
   }

   public void setLifeSpan(int lifeSpan) {
       this.lifeSpan = lifeSpan;
   }

   public String getSize() {
       return size;
   }

   public void setSize(String size) {
       this.size = size;
   }

   public int getNoOfLegs() {
       return noOfLegs;
   }

   public void setNoOfLegs(int noOfLegs) {
       this.noOfLegs = noOfLegs;
   }

   /**
   * ToString method
   */
   public String toString(){
       String retString = "";
       retString ="Animal : "+this.getName() +" Lifespan : "+this.getLifeSpan() +" Size : "+this.getSize()+" No of legs: " +this.getNoOfLegs();
       return retString;
   }

}

AnimalTester.java


public class AnimalTester {

   public static void main(String [] args){
       //1. Creating Animal object Elephent
       Animal elephent = new Animal("Elephent",20, "Big", 4);
      
       //2. Creating Animal object cat
       Animal cat = new Animal("Cat",5, "Big", 4);
      
      
      
       //Dispalying elephent and cat objects
       System.out.println(elephent.toString()); //This will show properties related to elephent object
       System.out.println(cat.toString()); //This will show properties related to cat object
   }

}

Sample output:

Animal : Elephent Lifespan : 20 Size : Big No of legs: 4
Animal : Cat Lifespan : 5 Size : Big No of legs: 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote