-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDog.java
More file actions
31 lines (22 loc) · 900 Bytes
/
Copy pathDog.java
File metadata and controls
31 lines (22 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package Online_Code_Samples.Week2;
public class Dog {
private String name; //achieve encapsulation by declaring variables as private
private int age;
public Dog(String name, int age) { //Class constructor
this.name = name;
this.age = age;
}
public String getName() { //getter method to access the private variable ‘name’. It’s return type
return name; //is String
}
public void bark() { //void methods do not return any value and do not have a return type
System.out.println("Woof!");
}
public static void main(String[] args) {
Dog dog = new Dog("Alsatian", 2); //A dog object was created with name and age
dog.bark(); //We use the dog object created to call the method 'bark()'
}
public String toString() {
return String.format("Dog name: %s, age: %d", this.name, this.age);
}
}