Skip to content

Commit d45a891

Browse files
ochejazhendrikse
authored andcommitted
BAEL-1261 Polymorphism in Java (eugenp#2762)
* Define beans for handling different message types in a lean chat app * Add class based spring beans configuration * Define spring configuration in XML for constructor based bean injection * Refactor package structure to separate constructor based bean injection code set from setter based bean injection code set * Define configuration and classes specific to setter-based bean injection. * Implement tests for constructor-based and setter-based bean injections * develop codes for explaining type erasure * Write unit tests for type erasure examples * Remove evaluation article code * Modify type erasure examples and unit tests * Modify type erasure examples and unit tests * Add expected exception in TypeErasureUnitTest * Correct grammar in class name * Implement File Manager app to demonstrate Polymorphism. Develop unit tests for Polymorphism article code * Add examples for static polymorphism * Change sysout statments to slf4j log info statements * Add assertions and expected errors check on Test * Add assertions and expected errors check on Test * Correct compile time error of symbol not found * Removed commented out non-compiling test.
1 parent 58f8e61 commit d45a891

5 files changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.polymorphism;
2+
3+
import java.awt.image.BufferedImage;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class FileManager {
9+
10+
final static Logger logger = LoggerFactory.getLogger(FileManager.class);
11+
12+
public static void main(String[] args) {
13+
GenericFile file1 = new TextFile("SampleTextFile", "This is a sample text content", "v1.0.0");
14+
logger.info("File Info: \n" + file1.getFileInfo() + "\n");
15+
ImageFile imageFile = new ImageFile("SampleImageFile", 200, 100, new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB).toString()
16+
.getBytes(), "v1.0.0");
17+
logger.info("File Info: \n" + imageFile.getFileInfo());
18+
}
19+
20+
public static ImageFile createImageFile(String name, int height, int width, byte[] content, String version) {
21+
ImageFile imageFile = new ImageFile(name, height, width, content, version);
22+
logger.info("File 2 Info: \n" + imageFile.getFileInfo());
23+
return imageFile;
24+
}
25+
26+
public static GenericFile createTextFile(String name, String content, String version) {
27+
GenericFile file1 = new TextFile(name, content, version);
28+
logger.info("File 1 Info: \n" + file1.getFileInfo() + "\n");
29+
return file1;
30+
}
31+
32+
public static TextFile createTextFile2(String name, String content, String version) {
33+
TextFile file1 = new TextFile(name, content, version);
34+
logger.info("File 1 Info: \n" + file1.getFileInfo() + "\n");
35+
return file1;
36+
}
37+
38+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.polymorphism;
2+
3+
import java.util.Date;
4+
5+
public class GenericFile {
6+
private String name;
7+
private String extension;
8+
private Date dateCreated;
9+
private String version;
10+
private byte[] content;
11+
12+
public GenericFile() {
13+
this.setDateCreated(new Date());
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getExtension() {
25+
return extension;
26+
}
27+
28+
public void setExtension(String extension) {
29+
this.extension = extension;
30+
}
31+
32+
public Date getDateCreated() {
33+
return dateCreated;
34+
}
35+
36+
public void setDateCreated(Date dateCreated) {
37+
this.dateCreated = dateCreated;
38+
}
39+
40+
public String getVersion() {
41+
return version;
42+
}
43+
44+
public void setVersion(String version) {
45+
this.version = version;
46+
}
47+
48+
public byte[] getContent() {
49+
return content;
50+
}
51+
52+
public void setContent(byte[] content) {
53+
this.content = content;
54+
}
55+
56+
public String getFileInfo() {
57+
return "File Name: " + this.getName() + "\n" + "Extension: " + this.getExtension() + "\n" + "Date Created: " + this.getDateCreated() + "\n" + "Version: " + this.getVersion() + "\n";
58+
}
59+
60+
public Object read() {
61+
return content;
62+
}
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.polymorphism;
2+
3+
public class ImageFile extends GenericFile {
4+
private int height;
5+
private int width;
6+
7+
public ImageFile(String name, int height, int width, byte[] content, String version) {
8+
this.setHeight(height);
9+
this.setWidth(width);
10+
this.setContent(content);
11+
this.setName(name);
12+
this.setVersion(version);
13+
this.setExtension(".jpg");
14+
}
15+
16+
public int getHeight() {
17+
return height;
18+
}
19+
20+
public void setHeight(int height) {
21+
this.height = height;
22+
}
23+
24+
public int getWidth() {
25+
return width;
26+
}
27+
28+
public void setWidth(int width) {
29+
this.width = width;
30+
}
31+
32+
public String getFileInfo() {
33+
return super.getFileInfo() + "Height: " + this.getHeight() + "\n" + "Width: " + this.getWidth();
34+
}
35+
36+
public String read() {
37+
return this.getContent()
38+
.toString();
39+
}
40+
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.polymorphism;
2+
3+
public class TextFile extends GenericFile {
4+
private int wordCount;
5+
6+
public TextFile(String name, String content, String version) {
7+
String[] words = content.split(" ");
8+
this.setWordCount(words.length > 0 ? words.length : 1);
9+
this.setContent(content.getBytes());
10+
this.setName(name);
11+
this.setVersion(version);
12+
this.setExtension(".txt");
13+
}
14+
15+
public int getWordCount() {
16+
return wordCount;
17+
}
18+
19+
public void setWordCount(int wordCount) {
20+
this.wordCount = wordCount;
21+
}
22+
23+
public String getFileInfo() {
24+
return super.getFileInfo() + "Word Count: " + wordCount;
25+
}
26+
27+
public String read() {
28+
return this.getContent()
29+
.toString();
30+
}
31+
32+
public String read(int limit) {
33+
return this.getContent()
34+
.toString()
35+
.substring(0, limit);
36+
}
37+
38+
public String read(int start, int stop) {
39+
return this.getContent()
40+
.toString()
41+
.substring(start, stop);
42+
}
43+
44+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.polymorphism;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.awt.image.BufferedImage;
6+
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
10+
public class PolymorphismUnitTest {
11+
12+
@Test
13+
public void givenImageFile_whenFileCreated_shouldSucceed() {
14+
ImageFile imageFile = FileManager.createImageFile("SampleImageFile", 200, 100, new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB).toString()
15+
.getBytes(), "v1.0.0");
16+
assertEquals(200, imageFile.getHeight());
17+
}
18+
19+
// Downcasting then Upcasting
20+
@Test
21+
public void givenTextFile_whenTextFileCreatedAndAssignedToGenericFileAndCastBackToTextFileOnGetWordCount_shouldSucceed() {
22+
GenericFile textFile = FileManager.createTextFile("SampleTextFile", "This is a sample text content", "v1.0.0");
23+
TextFile textFile2 = (TextFile) textFile;
24+
assertEquals(6, textFile2.getWordCount());
25+
}
26+
27+
// Downcasting
28+
@Test(expected = ClassCastException.class)
29+
public void givenGenericFile_whenCastToTextFileAndInvokeGetWordCount_shouldFail() {
30+
GenericFile genericFile = new GenericFile();
31+
TextFile textFile = (TextFile) genericFile;
32+
System.out.println(textFile.getWordCount());
33+
}
34+
}

0 commit comments

Comments
 (0)