aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Class concept Explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Class concept Explanation" Watch "Class concept Explanation" New topic
Author

Class concept Explanation

anuj thite
Ranch Hand

Joined: Apr 14, 2009
Posts: 49
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

    Joined: Sep 08, 2007
    Posts: 3068
        
      33

    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)

    Mohamed Sanaulla | My Blog
    anuj thite
    Ranch Hand

    Joined: Apr 14, 2009
    Posts: 49
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Class concept Explanation