Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this is code goes in a file called Animal.java
- //it is called a stand-alone class
- //it can NOT BE RUN
- //if try and RUN you will get a Error Message saying no Main Method
- //stand-alone classes are the blueprint of the objects
- //the main method runs. and creates the Animal object
- public class Animal {
- private String diet; //instance variable - must be private
- //must be defined first in class
- //constructor to set and create object
- //this is a reserved word which mean this object
- // you must use to distinguish between instance var diet and the passed diet
- public Animal (String diet) {
- this.diet=diet;
- }
- //accessor method -- returns back the diet for this Animal
- public String getDiet() { //method header .. must be public so it can be called from main method
- //after public what it returns which is a String.. then the name of the method
- //accessor method by style always start with verb get .. the () are the param list -- the input
- //no input needed for an accessor method..
- return diet; //returns the instance var. String diet
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement