Advertisement
Guest User

Animal

a guest
Apr 8th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. //this is code goes in a file called Animal.java
  2. //it is called a stand-alone class
  3. //it can NOT BE RUN
  4. //if try and RUN you will get a Error Message saying no Main Method
  5. //stand-alone classes are the blueprint of the objects
  6. //the main method runs. and creates the Animal object
  7.  
  8. public class Animal {
  9.     private String diet; //instance variable - must be private
  10.                  //must be defined first in class
  11.  
  12.                 //constructor to set and create object
  13.                 //this is a reserved word which mean this object
  14.                 // you must use to distinguish between instance var diet and the passed diet
  15.     public Animal (String diet) {
  16.                       this.diet=diet;
  17.                  }
  18.  
  19.                 //accessor method -- returns back the diet for this Animal
  20.                 public String getDiet() { //method header .. must be public so it can be called from main method
  21.                     //after public what it returns which is a String.. then the name of the method
  22.                     //accessor method by style always start with verb get  .. the () are the param list -- the input
  23.                     //no input needed for an accessor method..
  24.        return diet;   //returns the instance var. String diet
  25.                 }
  26.  
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement