diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-01-07 18:51:03 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-01-07 18:51:03 +0100 |
commit | 9240342b366db9999f11659a1f1c396ba418ad78 (patch) | |
tree | f34e9259cbb6f8d4bcdea234e4b2c927950411cc /Year_2/SE/error_inheritance | |
parent | ba36beaec6d37d26b075d96e58aad73151d6d39e (diff) |
Adds
Diffstat (limited to 'Year_2/SE/error_inheritance')
-rw-r--r-- | Year_2/SE/error_inheritance/Main.java | 19 | ||||
-rw-r--r-- | Year_2/SE/error_inheritance/Person.java | 10 | ||||
-rw-r--r-- | Year_2/SE/error_inheritance/Student.java | 13 |
3 files changed, 42 insertions, 0 deletions
diff --git a/Year_2/SE/error_inheritance/Main.java b/Year_2/SE/error_inheritance/Main.java new file mode 100644 index 0000000..206a513 --- /dev/null +++ b/Year_2/SE/error_inheritance/Main.java @@ -0,0 +1,19 @@ +public class Main { + public static void main(String[] args) { + Student s = new Student(); + s.setName("Santo"); + s.setMatr("1948"); + + s.print(); + + Person p = s; + // That's not compile + // p.setMatr("1949"); + p.setName("John"); + p.print(); + + // s.name is equals to p.name, so the code below print + // Hello, John[...] + s.print(); + } +} diff --git a/Year_2/SE/error_inheritance/Person.java b/Year_2/SE/error_inheritance/Person.java new file mode 100644 index 0000000..2e52d79 --- /dev/null +++ b/Year_2/SE/error_inheritance/Person.java @@ -0,0 +1,10 @@ +public class Person { + private String name; + public void setName(String n) { + this.name = n; + } + + public void print() { + System.out.println("Hello, "+ this.name); + } +} diff --git a/Year_2/SE/error_inheritance/Student.java b/Year_2/SE/error_inheritance/Student.java new file mode 100644 index 0000000..1789489 --- /dev/null +++ b/Year_2/SE/error_inheritance/Student.java @@ -0,0 +1,13 @@ +public class Student extends Person { + private String matr; + + public void setMatr(String m) { + this.matr = m; + } + + @Override + public void print() { + super.print(); + System.out.println("I'm "+ this.matr); + } +} |