From 604c45b8f0b8f4b175df6f08a2eaf6b9faa1af9b Mon Sep 17 00:00:00 2001 From: Michael Olayemi Date: Fri, 13 Oct 2023 12:06:18 +0000 Subject: [PATCH] Difference Between Information Hiding and Encapsulation (#14886) * Difference Between Information Hiding and Encapsulation * Difference Between Information Hiding and Encapsulation --- .../java/com/baeldung/encapsulation/Book.java | 7 ++++ .../baeldung/encapsulation/BookDetails.java | 8 +++++ .../encapsulation/BookEncapsulation.java | 17 +++++++++ .../encapsulation/BookInformationHiding.java | 36 +++++++++++++++++++ ...psulationAndInformationHidingUnitTest.java | 35 ++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/Book.java create mode 100644 core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookDetails.java create mode 100644 core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookEncapsulation.java create mode 100644 core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookInformationHiding.java create mode 100644 core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/encapsulation/EncapsulationAndInformationHidingUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/Book.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/Book.java new file mode 100644 index 0000000000..73762758e5 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/Book.java @@ -0,0 +1,7 @@ +package com.baeldung.encapsulation; + +public class Book { + public String author; + public int isbn; + +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookDetails.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookDetails.java new file mode 100644 index 0000000000..9df45b1f14 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookDetails.java @@ -0,0 +1,8 @@ +package com.baeldung.encapsulation; + +public class BookDetails { + + public String bookDetails(Book book) { + return "author name: " + book.author + " ISBN: " + book.isbn; + } +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookEncapsulation.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookEncapsulation.java new file mode 100644 index 0000000000..c39ca4bab4 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookEncapsulation.java @@ -0,0 +1,17 @@ +package com.baeldung.encapsulation; + +public class BookEncapsulation { + public String author; + public int isbn; + public int id = 1; + + public BookEncapsulation(String author, int isbn) { + this.author = author; + this.isbn = isbn; + } + + public String getBookDetails() { + return "author id: " + id + " author name: " + author + " ISBN: " + isbn; + } + +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookInformationHiding.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookInformationHiding.java new file mode 100644 index 0000000000..2d670a358d --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/encapsulation/BookInformationHiding.java @@ -0,0 +1,36 @@ +package com.baeldung.encapsulation; + +public class BookInformationHiding { + private String author; + private int isbn; + private int id = 1; + + public BookInformationHiding(String author, int isbn) { + setAuthor(author); + setIsbn(isbn); + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public int getIsbn() { + return isbn; + } + + public void setIsbn(int isbn) { + if (isbn < 0) { + throw new IllegalArgumentException("ISBN can't be negative"); + } + this.isbn = isbn; + } + + public String getBookDetails() { + return "author id: " + id + " author name: " + author + " ISBN: " + isbn; + } + +} diff --git a/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/encapsulation/EncapsulationAndInformationHidingUnitTest.java b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/encapsulation/EncapsulationAndInformationHidingUnitTest.java new file mode 100644 index 0000000000..6106d1a406 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/encapsulation/EncapsulationAndInformationHidingUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.encapsulation; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class EncapsulationAndInformationHidingUnitTest { + + @Test + public void givenUnencapsulatedClass_whenImplementationClassIsSeparate_thenReturnResult() { + Book myBook = new Book(); + myBook.author = "J.K Rowlings"; + myBook.isbn = 67890; + BookDetails details = new BookDetails(); + String result = details.bookDetails(myBook); + assertEquals("author name: " + myBook.author + " ISBN: " + myBook.isbn, result); + + } + + @Test + public void givenEncapsulatedClass_whenDataIsNotHidden_thenReturnResult() { + BookEncapsulation myBook = new BookEncapsulation("J.K Rowlings", 67890); + String result = myBook.getBookDetails(); + assertEquals("author id: " + 1 + " author name: " + myBook.author + " ISBN: " + myBook.isbn, result); + } + + @Test + public void givenEncapsulatedClass_whenDataIsHidden_thenReturnResult() { + BookInformationHiding myBook = new BookInformationHiding("J.K Rowlings", 67890); + String result = myBook.getBookDetails(); + assertEquals("author id: " + 1 + " author name: " + myBook.getAuthor() + " ISBN: " + myBook.getIsbn(), result); + } + +}