---
title: "Encapsulation in OOP?"  
description: "Encapsulation in OOP?"  
author: "Anonymous User"  
published: 2013-06-12  
updated: 2013-06-12  
canonical: https://www.mindstick.com/forum/1053/encapsulation-in-oop  
category: "oops"  
tags: ["oops"]  
reading_time: 3 minutes  

---

# Encapsulation in OOP?

I am studying about [OOP](https://www.mindstick.com/forum/160066/what-is-oop-in-javascript) concepts. As I understood from the [documents](https://www.mindstick.com/articles/156931/the-main-types-of-business-documents) I have read, I wrote an example [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) for [encapsulation](https://www.mindstick.com/articles/12229/encapsulation-and-access-specifier) [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) in OOP. I pasted my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) below. Is my concept about encapsulation is correct ?.

\
**Default.aspx**

```
<asp:Button ID="showBtn" Text="Show Student details." runat="server"/>
```

\
**Default.aspx.cs**

```
public partial class _Default : System.Web.UI.Page{Student stu; protected void Page_Load(object sender, EventArgs e){    stu = new Student();    stu.SetStudentID(001);    stu.SetStudentFee(5000);    stu.StudentName = "Rob";    stu.StudentAge = 26;     showBtn.Click += new EventHandler(showBtn_Click);} void showBtn_Click(object sender, EventArgs e){    stu.ShowStudentDetails();}}
```

\

\

**[Class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) [Student](https://www.mindstick.com/articles/157138/student-loan-default-wage-garnishment-and-student-loan)**

**\**

```
class Student{private int stuId;private int stuFee;private string stuName;private int stuAge; public void SetStudentID(int id){    stuId = id; //Mutator} public void SetStudentFee(int fee){    stuFee = fee;  //Mutator} public int GetStudentID(){    return stuId;  //Accessor} public int GetStudentFee(){    return stuFee;  //Accessor} public string StudentName{    get { return stuName; }   //Accessor    set { stuName = value; }  //Mutator} public int StudentAge{    get { return stuAge; }  //Accessor    set { stuAge = value; } //Mutator} private void ShowDetails(){    HttpContext.Current.Response.Write(this.GetStudentID() + " : " + this.StudentName + " : " + this.StudentAge + " : " + this.GetStudentFee());} public void ShowStudentDetails(){    ShowDetails();}}
```

\

My main doubt is about the way I called the ShowDetails() [method](https://www.mindstick.com/forum/166/webservice-method) in the

Student. Is this a good way to hide the method ShowDetails() ?.

## Replies

### Reply by Vijay Shukla

From an OO perspective, your ShowDetails method is doing two very different things.

- Creating a string that represents the object
- Outputting the string to the HttpResponse.

Now The first task does belong to the class Student, you need to know what an Student is to be able to create a string that is representative of the object. In fact in .net this is such a common thing, there is in fact a "overridable" or "virtual" function called Object.ToString().

The second task has absolutely nothing to do with the class Student, and A LOT to do with strings and HttpResponses (and in this case how we get the HttpResponse, which is to get it from the HttpContext, which means we MUST be on a webserver in a HttpRequest). With all these assumptions, it is extremely unsafe in an all purpose "data" or "domain" class.

This is how I would refactor this.

```
class Student{    private int stuId;    private int stuFee;    private string stuName;    private int stuAge;     public void SetStudentID(int id)    {        stuId = id; //Mutator    }     public void SetStudentFee(int fee)    {        stuFee = fee;  //Mutator    }     public int GetStudentID()    {        return stuId;  //Accessor    }     public int GetStudentFee()    {        return stuFee;  //Accessor    }     public string StudentName    {        get { return stuName; }   //Accessor        set { stuName = value; }  //Mutator    }     public int StudentAge    {        get { return stuAge; }  //Accessor        set { stuAge = value; } //Mutator    }     public override string ToString()    {        return this.GetStudentID() + " : " +             this.StudentName + " : " +             this.StudentAge + " : " +             this.GetStudentFee();    }  }
```

\

and

```
public partial class _Default : System.Web.UI.Page{    Student stu;     protected void Page_Load(object sender, EventArgs e)    {        stu = new Student();        stu.SetStudentID(001);        stu.SetStudentFee(5000);        stu.StudentName = "Rob";        stu.StudentAge = 26;         showBtn.Click += new EventHandler(showBtn_Click);    }     void showBtn_Click(object sender, EventArgs e)    {        HttpContext.Current.Response.Write(stu.ToString());    }}
```

Since we KNOW for sure that there is a valid HttpContext.Current on the webpage. Thus Students don't need to know about the internet, and would equally be able to work on a WinForm application.


---

Original Source: https://www.mindstick.com/forum/1053/encapsulation-in-oop

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
