-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializingObject.java
More file actions
74 lines (63 loc) · 2.11 KB
/
Copy pathInitializingObject.java
File metadata and controls
74 lines (63 loc) · 2.11 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package Phase3_ObjectOrientation.ClassesAndObject;
/**The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.
* The new operator also invokes the class constructor.
* <p>
*
* This class contains a single constructor. We can recognize a constructor because its declaration uses the same name as the class and it has no return type.
* The Java compiler differentiates the constructors based on the number and the type of the arguments. The constructor in the Dog class takes four arguments.
* The following statement provides “tuffy”,”papillon”,5,”white” as values for those arguments:
* <p>
*
* Dog tuffy = new Dog("tuffy","papillon",5, "white");**/
public class InitializingObject {
// Instance Basics.Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class
public InitializingObject(String name, String breed, int age, String color) {
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
// method 1
public String getName() {
return name;
}
// method 2
public String getBreed() {
return breed;
}
// method 3
public int getAge() {
return age;
}
// method 4
public String getColor() {
return color;
}
@Override
public String toString() {
return ("Hi my name is " + this.getName() +
".\nMy breed,age and color are " +
this.getBreed() + "," + this.getAge() +
"," + this.getColor());
}
public static void main(String[] args) {
InitializingObject tuffy = new InitializingObject("tuffy", "papillon", 5, "white");
System.out.println(tuffy.getName());
System.out.println(tuffy.getBreed());
System.out.println(tuffy.getAge());
System.out.println(tuffy.getColor());
System.out.println(tuffy.toString());
}
//OUTPUT
//tuffy
//papillon
//5
//white
//Hi my name is tuffy.
//My breed,age and color are papillon,5,white
}