From 8f0f41ecea32c7e2d3294203963c5117da23fca8 Mon Sep 17 00:00:00 2001 From: NirmalSilwal Date: Mon, 31 Aug 2020 08:57:47 +0530 Subject: [PATCH] constructor demo: blueprint of person class --- CodingBlocks Training/Day15/person3.java | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 CodingBlocks Training/Day15/person3.java diff --git a/CodingBlocks Training/Day15/person3.java b/CodingBlocks Training/Day15/person3.java new file mode 100644 index 0000000..135351e --- /dev/null +++ b/CodingBlocks Training/Day15/person3.java @@ -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; + } +}