November 23, 2014
Hot Topics:

Object Responsibility

  • May 24, 2005
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Compiler Enforcement

Finally, it is important to identify that a very powerful aspect of object-oriented development is that the language actually enforces certain rules via the compiler. The use of an abstract class provides the mechanism for the enforcement of the contract concept that you saw earlier. Consider the possibility that the Circle class inherits from Shape, yet fails to implement the draw( ) method.

public class Circle extends Shape{
   .. no draw() method
}

When this code is compiled with the Shape class above, the compiler produces the following error:

C:\TestShape\Circle.java:1: Circle should be declared abstract;
                            it does not define draw( ) in Shape
public class Circle extends Shape{
^
1 error

Thus, the Java compiler acts as an enforcer so that there can be no shape defined that does not provide a draw( ) method.

Conclusion

This column has focused on the key object-oriented concept of object responsibility. You started off by examining the mantra of "an object must be responsible for itself." In a nutshell, the concept of object responsibility and autonomy are key issues when designing a useful and secure object. The fact that objects are moved over networks makes object responsibility and autonomy even more important. One of the benefits of using an object-oriented language is that the compiler itself can enforce certain aspects pertaining to object responsibility through the use of polymorphism. Next month, you will explore other characteristics of proper object design.

References

  • Coad, Peter, and Mark Mayfield: Java Design. Object International, 1999.
  • Meyers, Scott: Effective C++. Addison-Wesley, 1992.
  • Gilbert, Stephen, and Bill McCarty: Object-Oriented Design in Java. The Waite Group, 1998.

About the Author

Matt Weisfeld is an Assistant Professor at Cuyahoga Community College (Tri-C) in Cleveland, Ohio. Matt is a part of the Information Technology department, teaching programming languages such as C++, Java, and C# .NET as well as various Web technologies. Prior to joining Tri-C, Matt spent 20 years in the information technology industry gaining experience in software development, project management, business development, corporate training, and part-time teaching. Matt holds an MS in computer science and an MBA in project management.

The material presented in these articles is based on material from the second edition of my book, The Object-Oriented Thought Process, 2nd edition. The Object-Oriented Thought Process is intended for anyone who needs to understand the basic object-oriented concepts before jumping into the code.



Page 4 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel