ENCAPSULATION IN OOP?

jacob rasel

Total Post:88

Points:616
Posted by  jacob rasel
OOPS 
 1603  View(s)
Ratings:
Rate this:

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() ?.

  1. Vijay Shukla

    Post:100

    Points:700
    Re: Encapsulation in OOP?

    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 inside the webpage. Thus Students don't need to know about the internet, and would equally be able to work on a WinForm application.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!