-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
constructor demo: blueprint of person class
- Loading branch information
1 parent
11f06ef
commit 8f0f41e
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |