Whenever I prepare for interview, I always think of this basic concept and always find it confusing. Well, may be because I always thought about both of the hiding implementations. Also, I was under impression that encapsulation complements abstraction. I always had a question, if both hides implementation and complements each other then why two different names or why Encapsulation Vs Abstraction.
In the section, given below, I am going to discuss both. As per my understanding, encapsulation refers to-
In the section, given below, I am going to discuss both. As per my understanding, encapsulation refers to-
Encapsulation
In OOPS, an encapsulation allows us to wrap and control access of the logically grouped data. Yes, encapsulation means hiding the data. Lets see the class, given below. Its a simple class of Student, which encapsulates student properties and methods.
Code
- public class Student
- {
- public string FirstName {get; set;}
- public string LastName {get; set;}
- public DateTime DateOfBirth {get; set;}
- public bool Add(Student student)
- {
- ........Some Code
- }
- public List All()
- {
- ........Some Code
- }
- public bool Delete(Student student)
- {
- ........Some Code
- }
- }
Suppose, we want to implement one more class for teachers. I used to do is to copy student class and replace all Student with Teacher, because I always know the students and teachers both are human and have similar properties and methods.
Code
Sarath MayaPosted Oct 5, 2016, 10:20 PM
I can't understand abstraction concept can you explain briefly
Satish Kumar VadlavalliPosted Sep 19, 2016, 11:26 PM
You could have extend the abstraction code till Student and teacher class
Satish Kumar VadlavalliPosted Sep 19, 2016, 11:26 PM
Nice yar :)
SubashPosted Sep 15, 2016, 12:30 AM
Good start