BAEL-5131: constructor specification is added c (#11586)
* BAEL-5131: constructor specification is added c * BAEL-5131: codes have been reformatted * BAEL-5131: I've added a few minor changes over on this package. * BAEL-5131: I've added a few minor changes over on this package.
This commit is contained in:
parent
7ad433dcb6
commit
db396b39a9
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.constructorspecification.rules;
|
||||
|
||||
class Person {
|
||||
|
||||
String name;
|
||||
|
||||
public Person() {
|
||||
this("Arash");
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.constructorspecification.superclass;
|
||||
|
||||
class Person {
|
||||
|
||||
int id;
|
||||
|
||||
public Person(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue