---
title: "Discussion on \"OOPS In Depth and Fundamentals of Design Patterns\" | Hashnode"
description: "Discussion on \"OOPS In Depth and Fundamentals of Design Patterns\". With Real-world Examples | Part 1"
url: "https://hashnode.com/posts/oops-in-depth-and-fundamentals-of-design-patterns/63847318c7115b40741126fd"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [OOPS In Depth and Fundamentals of Design Patterns](https://lovepreet.hashnode.dev/oops-in-depth-and-fundamentals-of-design-patterns?utm_source=hashnode&utm_medium=feed)

With Real-world Examples | Part 1

Nov 28, 2022 · 5 min read · lovepreet.hashnode.dev  
Updated Dec 14, 2022  
Read the article

-   [#programming-blogs](https://hashnode.com/tag/programming-blogs)
-   [#java](https://hashnode.com/tag/java)
-   [#javascript](https://hashnode.com/tag/javascript)
-   [#coding](https://hashnode.com/tag/coding)
-   [#beginners](https://hashnode.com/tag/beginners)

## Written by

[Lovepreet Singh](https://hashnode.com/@Singh5713)

@Singh5713 · Bangalore, India

  
[View profile](https://hashnode.com/@Singh5713)

Backend Engineer @CRED | System Design | Low Level Design | Algorithms

Backend Engineer @CRED | Open Source | IIT Gandhinagar | Computer Science

## Responses 2

Markdown supported · ⌘↵ to post

[Dave Roman](https://hashnode.com/@MrDave1999)

[Nov 28, 2022](https://hashnode.com/posts/oops-in-depth-and-fundamentals-of-design-patterns/63847318c7115b40741126fd/comment/6384ff41ddebe4230b224fa6)

First of all, thank you for sharing your knowledge with the community. I'm not entirely convinced by the explanation of encapsulation though.

By definition, encapsulation serves to hide implementation details from the outside (e.g. from other client classes). However, in the examples you provide, I don't see the definition being met:

```
public class Student{
  private String name;
  private int marks;
  Student(){}
  Student(String name, int marks){
    this.name = name;
    this.marks = marks;
  }

  public int getMarks(){
    return this.marks;
  }
  public String getName(){
    return this.name;
  }
  public void setName(String t_name){
    this.name = t_name;
  }
  public void setMarks(int t_marks){
    if(t_marks<=100 && t_marks>=0){
          this.marks = t_marks;
          return;
    }else{
      throw new Exception("Enter marks from 0-100 included");
    }
  }
}
```

In this example an implementation detail is being exposed, the actual types of the private fields. This breaks encapsulation, why? Let's imagine that tomorrow we decide that the type of the field "marks" will no longer be "int", but, "long", so we simply make the change:

```
public class Student{
  private String name;
  private long marks;
  Student(){}
  Student(String name, int marks){
    this.name = name;
    this.marks = marks;
  }

  public long getMarks(){
    return this.marks;
  }
  public String getName(){
    return this.name;
  }
  public void setName(String t_name){
    this.name = t_name;
  }
  public void setMarks(long t_marks){
    if(t_marks<=100 && t_marks>=0){
          this.marks = t_marks;
          return;
    }else{
      throw new Exception("Enter marks from 0-100 included");
    }
  }
}
```

1.  I had to change the return value type of the "getMarks" method, this could easily break the code of other client classes that have used the method.
2.  I also had to change the data type of the parameter of the "setMarks" method, this could easily break other parts of the code.

And this is just an example, of course, Java compilers can perform an implicit conversion from "int" to "long" but when working with complex types, the issue changes and can be more critical.

[Lovepreet Singh](https://hashnode.com/@Singh5713)

[Nov 29, 2022](https://hashnode.com/posts/oops-in-depth-and-fundamentals-of-design-patterns/63847318c7115b40741126fd/comment/6384ff41ddebe4230b224fa6#reply-6385adee47639d245e65a08c)

Encapsulation's main purpose is to combine the related fields and methods in a class or capsule. Data hiding is an extra layer above it. Methods we have to expose i.e. Getters and Setters we have to make sure that marks and name can be changed or set

[Taha Syed](https://hashnode.com/@tahamsyed)

[Nov 28, 2022](https://hashnode.com/posts/oops-in-depth-and-fundamentals-of-design-patterns/63847318c7115b40741126fd/comment/6384c4019537c92102ec0e9f)

Interesting...
