diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/Employee.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/Employee.java new file mode 100644 index 0000000000..cb90aa3253 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/Employee.java @@ -0,0 +1,24 @@ +package com.baeldung.constructorspecification.rules; + +class Employee extends Person { + + int id; + + public Employee() { + super(); + } + + public Employee(String name) { + super(name); + } + + public Employee(int id) { + this(); + //super("John"); // syntax error + this.id = id; + } + + public static void main(String[] args) { + new Employee(100); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/Person.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/Person.java new file mode 100644 index 0000000000..9d96afdb01 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/Person.java @@ -0,0 +1,14 @@ +package com.baeldung.constructorspecification.rules; + +class Person { + + String name; + + public Person() { + this("Arash"); + } + + public Person(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/RecursiveConstructorInvocation.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/RecursiveConstructorInvocation.java new file mode 100644 index 0000000000..85ab2b6872 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/rules/RecursiveConstructorInvocation.java @@ -0,0 +1,12 @@ +package com.baeldung.constructorspecification.rules; + +class RecursiveConstructorInvocation { + + public RecursiveConstructorInvocation() { + RecursiveConstructorInvocation rci = new RecursiveConstructorInvocation(); + } + + public static void main(String[] args) { + new RecursiveConstructorInvocation(); // java.lang.StackOverflowError + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/simple/Person.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/simple/Person.java new file mode 100644 index 0000000000..101df06150 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/simple/Person.java @@ -0,0 +1,14 @@ +package com.baeldung.constructorspecification.simple; + +class Person { + + String name; + + public Person() { + this("Arash"); //ExplicitConstructorInvocation + } + + public Person(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/superclass/Employee.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/superclass/Employee.java new file mode 100644 index 0000000000..884fc4e404 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/superclass/Employee.java @@ -0,0 +1,15 @@ +package com.baeldung.constructorspecification.superclass; + +class Employee extends Person { + + String name; + + public Employee(int id) { + super(id); //ExplicitConstructorInvocation + } + + public Employee(int id, String name) { + super(id); + this.name = name; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/superclass/Person.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/superclass/Person.java new file mode 100644 index 0000000000..24bde9f613 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructorspecification/superclass/Person.java @@ -0,0 +1,10 @@ +package com.baeldung.constructorspecification.superclass; + +class Person { + + int id; + + public Person(int id) { + this.id = id; + } +} \ No newline at end of file