Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Frog //Frog class, frog used as no better word could be thought of, nothing of significance.
- {
- private String name; //private objects can only be accessed from within the same class.
- private int age; //This is forcing encapsulation so that a classes properties can only be modified using the class (well... its methods).
- public void setName(String newName) //Creating a set method to change the name of the frog instance.
- {
- name = newName;
- }
- public void setAge(int newAge) //Creating a set method to change the age of the frog instance.
- {
- age = newAge;
- }
- public String getName() //Creating a get method to return the name of the frog instance.
- {
- return name;
- }
- public int getAge() //Creating a get method to return the age of the frog instance.
- {
- return age;
- }
- }
- public class Application
- {
- public static void main(String[] args)
- {
- Frog frog1 = new Frog();
- //frog1.name = "Bertie"; //This code will not work because the variables are set to private meaning they are not accessible from this place in the code.
- //frog1.age = 1; //Same case with this line.
- frog1.setName("Bertie"); //This is the same as the code on line 27. Except this calls a method to change the property.
- frog1.setAge(1);
- System.out.println(frog1.getName());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement