forum

Home / DeveloperSection / Forums / Encapsulation in OOP?

Encapsulation in OOP?

Anonymous User255612-Jun-2013

I am studying about OOP concepts. As I understood from the documents I have read, I wrote an example program for encapsulation concept in OOP. I pasted my code 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 Student


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 in the

Student. Is this a good way to hide the method ShowDetails() ?.


Updated on 12-Jun-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By