Skip to content

Commit

Permalink
constructor demo: blueprint of person class
Browse files Browse the repository at this point in the history
  • Loading branch information
NirmalSilwal committed Aug 31, 2020
1 parent 11f06ef commit 8f0f41e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions CodingBlocks Training/Day15/person3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package Lecture15;

public class person3 {

private String name;
private int age;

// default constructor
person3() {
System.out.println("Hi everyone, default constructor is initialized!");
System.out.println("setting default values");
name = "defaultName";
age = 10;
}

// parameterized constructor
person3(String name, int age) {
System.out.println("Hi there, parameterized constructor is initialized!");
System.out.println("setting given parameter values in instance variables");
this.name = name;
this.age = age;
}

public int getAge() {
return this.age;
}

public String getName() {
return this.name;
}

public void setAge(int myage) {
if (myage < 0) {
return;
}
this.age = myage;
}

public void setName(String name) {
this.name = name;
}
}

0 comments on commit 8f0f41e

Please sign in to comment.