• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class concept Explanation

 
anuj thite
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I do have some experience in object oriented programming.
I know the concept of class clearly.
But, today morning while doing surfing, as it is my daily routine, i found that in wikipedia, it is written for class that "Class is a cohesive package".
So, Specifically I would like to know here is
  • what Cohesion actually means in OOPS ? &
  • what Coupling means in OOPS ?
  • How they are related with each other?


  • Please explain me in details by giving some sample examples.
    Your Help is really appreciated.
    Thanks in advance.


     
    Mohamed Sanaulla
    Saloon Keeper
    Posts: 3159
    33
    Google App Engine Java Ruby
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Cohesion is the degree to which the class is focused one its responsibilities- Highly cohesive classes are desirable.

    Lets consider this situation: We have a Student who enrolls for certain subjects as part of the academic requirements. So a Student will have a list of Subjects and other personal details

    Now if you see that the classes are specific to what ever responsibilities assigned. Lets make the class Student- less cohesive- lets add one method- getTeacherForSubject() in Student class.


    Now you see- its not part of Student to get the name of the teacher for a given subject. Its the responsibility of the Subject to return the name of the teacher. This way the class Student is less cohesive.
    (I just thought of some example, hope this clears up the concept)

    Coupling: Degree to which the classes are related to each other- Low coupling is desired. If the classes are very closely related to each other- one possibility of this happening is when one class tries to access the fields of other class without using a public method. So any change in the internal implementation of this class will break the other class which was relying on it. Lets use the same example given above.


    Now the requirement changes- its said that the numberOfCredits will be calculated based on the numberOfHoursPerWeek. So what happens is that there will be a getter provided in the Subject class to get the number of credits based on the number of hours per week. Now introducing this method breaks the rest of the other classes using the numberOfCredits field directly. So in order to avoid that classes should not be tightly related to each other. They should interact with each other via the public methods only. So the changed class definition would be


    Hope these examples help you to understand the concept.

    I had written a blog post related to this long back (Sorry for referring to my own blog)
     
    anuj thite
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mohamed Sanaulla wrote:Cohesion is the degree to which the class is focused one its responsibilities- Highly cohesive classes are desirable.

    Lets consider this situation: We have a Student who enrolls for certain subjects as part of the academic requirements. So a Student will have a list of Subjects and other personal details

    Now if you see that the classes are specific to what ever responsibilities assigned. Lets make the class Student- less cohesive- lets add one method- getTeacherForSubject() in Student class.


    Now you see- its not part of Student to get the name of the teacher for a given subject. Its the responsibility of the Subject to return the name of the teacher. This way the class Student is less cohesive.
    (I just thought of some example, hope this clears up the concept)

    Coupling: Degree to which the classes are related to each other- Low coupling is desired. If the classes are very closely related to each other- one possibility of this happening is when one class tries to access the fields of other class without using a public method. So any change in the internal implementation of this class will break the other class which was relying on it. Lets use the same example given above.


    Now the requirement changes- its said that the numberOfCredits will be calculated based on the numberOfHoursPerWeek. So what happens is that there will be a getter provided in the Subject class to get the number of credits based on the number of hours per week. Now introducing this method breaks the rest of the other classes using the numberOfCredits field directly. So in order to avoid that classes should not be tightly related to each other. They should interact with each other via the public methods only. So the changed class definition would be


    Hope these examples help you to understand the concept.

    I had written a blog post related to this long back (Sorry for referring to my own blog)


    Thanks Mr. Mohamed Sanaulla for the reply.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic