Newbie questions about class and methods
in
Programming Questions
•
1 year ago
Hi,
I'm new to processing and programming and I'm reading "Programming interactivity" by Joshua Noble.
There's something I did not understand completely about public and private methods in a class.
Public methods can be accessed outside the class, private methods can only be accessed within the class so if I tried to call a private method outside the class I should get an error. Is that right?
So why if I call my private method dream() outside the class I do not get any error? Here's my code:
I'm new to processing and programming and I'm reading "Programming interactivity" by Joshua Noble.
There's something I did not understand completely about public and private methods in a class.
Public methods can be accessed outside the class, private methods can only be accessed within the class so if I tried to call a private method outside the class I should get an error. Is that right?
So why if I call my private method dream() outside the class I do not get any error? Here's my code:
- class Dog {
- String name;
- int age;
- int weight;
- Dog () {
- age = 1;
- weight = 20;
- }
- public void giveDogAName (String _name) {
- name = _name;
- }
- public void bark() {
- println (name + " " + "says: wof wof");
- }
- public void sleep () {
- print (name + " " + "is dreaming" + " ");
- dream ("a bone");
- }
- private void dream (String dogDream) {
- println (dogDream);
- }
- }
- void setup () {
- Dog redolf = new Dog ();
- redolf.giveDogAName ("redolf");
- redolf.bark();
- redolf.sleep();
- redolf.dream ("a flower");
- }
1