Skip to content

Commit fb62444

Browse files
Composition VS Inheritance
Equals VS Referance String Pooling
1 parent df6396e commit fb62444

File tree

14 files changed

+508
-8
lines changed

14 files changed

+508
-8
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,23 @@
88
<artifactId>java-features-examples</artifactId>
99
<version>1.0-SNAPSHOT</version>
1010

11+
<build>
12+
13+
<plugins>
14+
<plugin>
15+
<groupId>org.apache.maven.plugins</groupId>
16+
<artifactId>maven-compiler-plugin</artifactId>
17+
<version>3.8.0</version>
18+
<configuration>
19+
<release>11</release>
20+
<source>8</source>
21+
<target>8</target> <!--or <release>10</release>-->
22+
</configuration>
23+
</plugin>
24+
</plugins>
25+
26+
</build>
27+
28+
1129

1230
</project>

src/main/java/tr/salkan/code/java/pure/examples/compositionVsinheritance/CompositionVsInheritanceMain.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tr.salkan.code.java.pure.examples.compositionVsinheritance.composition;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class CityExample {
7+
8+
static Set<String> set = new HashSet<>();
9+
10+
public static void main(String[] args) {
11+
12+
set.add("İstanbul");
13+
set.add("Ankara");
14+
set.add("Konya");
15+
set.forEach(System.out::println);
16+
}
17+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package tr.salkan.code.java.pure.examples.compositionVsinheritance.composition;
2+
3+
public class HumanExample {
4+
5+
6+
public static void main(String[] args) {
7+
8+
Hand hand = new Hand(true);
9+
Foot foot = new Foot(43);
10+
Face face = new Face(true);
11+
12+
Human human = new Human(hand,foot,face);
13+
14+
System.out.println(human);
15+
}
16+
17+
static class Human {
18+
19+
Hand hand;
20+
Foot foot;
21+
Face face;
22+
23+
public Human(Hand hand, Foot foot, Face face) {
24+
this.hand = hand;
25+
this.foot = foot;
26+
this.face = face;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "Human{" +
32+
"hand clearing =" + hand.isClear +
33+
", foot number =" + foot.footNumber +
34+
", face has beard =" + face.hasBeard +
35+
'}';
36+
}
37+
}
38+
39+
40+
static class Hand {
41+
42+
boolean isClear = false;
43+
44+
public Hand(boolean isClear) {
45+
this.isClear = isClear;
46+
}
47+
48+
public boolean isClear() {
49+
return isClear;
50+
}
51+
52+
public void setClear(boolean clear) {
53+
isClear = clear;
54+
}
55+
}
56+
57+
static class Foot {
58+
59+
int footNumber = 0;
60+
61+
public Foot(int footNumber) {
62+
this.footNumber = footNumber;
63+
}
64+
65+
public int getFootNumber() {
66+
return footNumber;
67+
}
68+
69+
public void setFootNumber(int footNumber) {
70+
this.footNumber = footNumber;
71+
}
72+
}
73+
74+
static class Face {
75+
76+
boolean hasBeard = false;
77+
78+
public Face(boolean hasBeard) {
79+
this.hasBeard = hasBeard;
80+
}
81+
82+
public boolean isHasBeard() {
83+
return hasBeard;
84+
}
85+
86+
public void setHasBeard(boolean hasBeard) {
87+
this.hasBeard = hasBeard;
88+
}
89+
}
90+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tr.salkan.code.java.pure.examples.compositionVsinheritance.inheritance;
2+
3+
import java.util.HashSet;
4+
5+
public class CityExample extends HashSet<Object> {
6+
7+
8+
public static void main(String[] args) {
9+
10+
CityExample cities = new CityExample();
11+
cities.add("İstanbul");
12+
cities.add("Ankara");
13+
cities.add("Konya");
14+
15+
cities.forEach(System.out::println);
16+
}
17+
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tr.salkan.code.java.pure.examples.compositionVsinheritance.inheritance;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Human {
7+
8+
public void borningInWhere(String location){
9+
10+
System.out.println("Human borning somewhere");
11+
}
12+
13+
public void eatingSomething(String food)
14+
{
15+
System.out.println("Human eating something");
16+
}
17+
18+
List<String> getBodyActivities()
19+
{
20+
List<String> bodyActivities = new ArrayList<>();
21+
22+
bodyActivities.add("Running");
23+
bodyActivities.add("Walking");
24+
bodyActivities.add("Jumping");
25+
26+
return bodyActivities;
27+
}
28+
29+
List<String> getMentalActivities() {
30+
31+
List<String> mentalActivities = new ArrayList<>();
32+
33+
mentalActivities.add("Thinking");
34+
mentalActivities.add("Estimating");
35+
mentalActivities.add("Imaging");
36+
37+
return mentalActivities;
38+
}
39+
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tr.salkan.code.java.pure.examples.compositionVsinheritance.inheritance;
2+
3+
public class HumanExample {
4+
5+
public static void main(String[] args) {
6+
7+
Human turkishMan = new TurkishMan();
8+
9+
turkishMan.borningInWhere("Turkey"); //overide
10+
turkishMan.eatingSomething("Kebap"); //override
11+
turkishMan.getBodyActivities().forEach(System.out::println); //super method
12+
13+
((TurkishMan)turkishMan).turkishManNameExample("Ahmet"); // method in TurkishMan class
14+
15+
Human italianMan = new ItalianMan();
16+
17+
turkishMan.borningInWhere("Italy"); //overide
18+
turkishMan.eatingSomething("Spaghetti"); //overide
19+
italianMan.getMentalActivities().forEach(System.out::println); //super method
20+
21+
((ItalianMan)italianMan).italianManNameExample("Del Pierro"); // method in ItalianMan class
22+
23+
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tr.salkan.code.java.pure.examples.compositionVsinheritance.inheritance;
2+
3+
public class ItalianMan extends Human {
4+
5+
@Override
6+
public void borningInWhere(String location) {
7+
System.out.println("In general a Italian man born in " + location);
8+
}
9+
10+
@Override
11+
public void eatingSomething(String food) {
12+
13+
System.out.println("In general a Turkish man eating " + food);
14+
}
15+
16+
public void italianManNameExample(String name)
17+
{
18+
System.out.println(name);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tr.salkan.code.java.pure.examples.compositionVsinheritance.inheritance;
2+
3+
public class TurkishMan extends Human {
4+
5+
6+
@Override
7+
public void borningInWhere(String location) {
8+
System.out.println("In general a Turkish man born in " + location);
9+
}
10+
11+
@Override
12+
public void eatingSomething(String food) {
13+
System.out.println("In general a Turkish man eating " + food);
14+
}
15+
16+
public void turkishManNameExample(String name)
17+
{
18+
System.out.println(name);
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tr.salkan.code.java.pure.examples.equalsVSreferance;
2+
3+
public class Man extends Person {
4+
5+
public Man(String name, int age) {
6+
super(name, age);
7+
}
8+
9+
@Override
10+
public boolean equals(Object o) {
11+
return super.equals(o);
12+
}
13+
14+
@Override
15+
public int hashCode() {
16+
return super.hashCode();
17+
}
18+
}

0 commit comments

Comments
 (0)