667481 members! Sign up to stay informed.

Sponsored Links


Resources

Enterprise Java
Research Library

Get Java white papers, product information, case studies and webcasts

News News News Messages: 8 Messages: 8 Messages: 8 Printer friendly Printer friendly Printer friendly Post reply Post reply Post reply XML XML XML

Object Slicing and Component Design with Java

Posted by: Nuno Teixeira on September 13, 2004 DIGG
Object slicing is defined as the conversion of an object into something with less information (typically a superclass). In C++ it occurs when an object is passed by value and copying the parameter value results in an upcast and it is considered a bad thing, as it may result in very subtle bugs. Object slicing throws away information. But in some situations this may be exactly what we want.

This article discusses how Object slicing can help with component design. It uses examples to get the message across.

Read Object Slicing and Component Design with Java

Threaded replies

·  Object Slicing and Component Design with Java by Nuno Teixeira on Mon Sep 13 16:03:14 EDT 2004
  ·  No article is available by David Rabinowitz on Tue Sep 14 02:10:55 EDT 2004
    ·  Article slicing by Ronald Jamaer on Tue Sep 14 06:49:01 EDT 2004
  ·  Diagrams are missing by Constantin Gonciulea on Tue Sep 14 10:25:30 EDT 2004
  ·  Very nice article by Mark van der Kraan on Tue Sep 14 17:53:51 EDT 2004
  ·  What is the benefit? by Mert Nuhoglu on Thu Sep 16 11:05:03 EDT 2004
    ·  What is the benefit? by Constantin Gonciulea on Thu Sep 16 16:00:05 EDT 2004
    ·  I agree. Also seperation is artificial by Jochen Bedersdorfer on Fri Sep 17 07:57:55 EDT 2004
  ·  The term "object slicing" by Chris Pearson on Sun Sep 13 21:56:52 EDT 2009
  Message #137728 Post reply Post reply Post reply Go to top Go to top Go to top

No article is available

Posted by: David Rabinowitz on September 14, 2004 in response to Message #137673
.

  Message #137768 Post reply Post reply Post reply Go to top Go to top Go to top

Article slicing

Posted by: Ronald Jamaer on September 14, 2004 in response to Message #137728
Article slicing

  Message #137808 Post reply Post reply Post reply Go to top Go to top Go to top

Diagrams are missing

Posted by: Constantin Gonciulea on September 14, 2004 in response to Message #137673
The UML diagrams are essential for understanding, and they are not showing up yet...

  Message #137900 Post reply Post reply Post reply Go to top Go to top Go to top

Very nice article

Posted by: Mark van der Kraan on September 14, 2004 in response to Message #137673
Well, it showed up for me, and I liked it very much.
Inner classes deserve more attention as a way to cleanly separate an object's responsibilities, and this article explained a particular example of that idea very well.
Cheers,
Mark

  Message #138202 Post reply Post reply Post reply Go to top Go to top Go to top

What is the benefit?

Posted by: Mert Nuhoglu on September 16, 2004 in response to Message #137673
Hi,

As much as I understand, the motivation behind object slicing is to decouple the persistence logic from the domain logic. This is useful for EJB architecture. But for Hibernate based applications, I can't see any need for object slicing. Because the persistent objects are unaware of the hibernate framework, when there is a DAO layer available.

Am I right? Or is there some other benefits too?

Thanks...

Mert Nuhoglu

  Message #138237 Post reply Post reply Post reply Go to top Go to top Go to top

What is the benefit?

Posted by: Constantin Gonciulea on September 16, 2004 in response to Message #138202
If the persistence (or any other kind of enhancement) methods are not in a subclass of the domain object, then there is no benefit. As a user of a persistence framework like Hibernate you are not concerned with the implementation details of the enahncing methods.

The technique does not apply to persistence only. It allows you to "stop the inheritance" if you need to. In C# this is the default (you redefine a method unless you use the override keyword) and upcasting may result in an object slicing. It's interesting that the C# designers chose as default the less common situation.

  Message #138305 Post reply Post reply Post reply Go to top Go to top Go to top

I agree. Also seperation is artificial

Posted by: Jochen Bedersdorfer on September 17, 2004 in response to Message #138202
As nice as separating business logic from "persistence logic" may be:
It just doesn't work out in real life.

Your code can ignore some aspects of persistence by using a clever
framework like JDO or hibernate, still you have to deal with, for example:
Removing objects - removing assocications between objects always bears
the question if one object also needs to be physically deleted.
(Aggregation vs. Association is such a case).

You may abstract away the specific technical details of persistence,
but your business logic has to deal with it one way or another.
Thus a seperation is IHMO artifical and serves no purpose.

  Message #321775 Post reply Post reply Post reply Go to top Go to top Go to top

The term "object slicing"

Posted by: Chris Pearson on September 13, 2009 in response to Message #137673
This article states that:

"Object slicing is defined as the conversion of an object into something with less information (typically a superclass)."

"In C++ it [object slicing] occurs when an object is passed by value and copying the parameter value results in an upcast and it is considered a bad thing, as it may result in very subtle bugs."

IMO "object slicing" is a vague street term, not a well-defined technical one. Too many web pages use the term incorrectly to describe simple assignment-by-value to a superclass instance from a subclass instance.

In any object oriented language, if a superclass object is initialized or assigned from a subclass object, it is expected and unavoidable that member variables defined in the subclass will not be copied to the superclass, for the simple reason that the superclass does not provide a place to store them.

That is not "object slicing" and is not an error or a problem -- it is exactly what one would expect. For example, a Shape object (superclass) might define Area and Color properties, while a Circle object (subclass) might additionally define a Radius property. A Shape object initialized from a Circle object will describe the Area and Color of the Circle, but not its Radius.

IMO, "object slicing" refers to the cases where the language unexpectedly copies only the superclass member variables to a subclass instance, as in the following C++ example:

Circle circ;
Shape &s = circ;
Circle circ2;
s = circ2; // circ2.Radius not copied to circ.Radius

This is quite a different case.

-- Chris

New content on TheServerSide.comNew content on TheServerSide.comNew content on TheServerSide.com

Dependency Injection in Java EE 6 - Part 1

Reza Rahman explores the features of the proposed JSR 299, Contexts and Dependency Injection for Java EE (CDI). When approved, it promises to be a key feature of Java EE 6. (November 2, Article)

SAML: It's Not just for Web services

SAML is an XML-based standard for exchanging authentication and authorization data between security domains. The single most important problem that SAML was created to solve is the Web browser Single Sign-On problem. Many organizations are debating whether to stay with version 1.1 or move to 2.0. This article makes observations about both options. (September 28, Article)

Programming is Also Teaching Your Team

Joe Ottinger takes a look at how people learn, and applies it to the practice of programming. He notes that understanding how people learn is an essential part of working in a programming team. (September 22, Article)

Can Java EE Deliver The Asynchronous Web?

Stephen Maryka gave us an article about the Asynchronous Web and posed a number of questions that get examined like an approach to delivering Asynchronous Web capabilities through extensions to existing Java EE technologies. (July 14, Article)

JSF Flex

JavaServer Faces Flex goal is to provide users capability in creating standard Flex components, part of flexSDK which is open sourced through MPL license, as normal JSF components. This article by Ji Hoon Kim will provide an overview of creating a simple multilingual JSF page consisting of JSF Flex tags. (June 29, Article)

The Rules of SOA - A Road to a Successful SOA Implementation

In this session Jeff explores the key characteristics of successful SOA projects. He covers some of the patterns, and anti-patterns, tool sets, and strategies that he himself learned the hard way. Last, he provides a strategy and blueprint for achieving a high likelihood of success in your SOA project. (June 23, Tech Talk)

Ari Zilka Talks About Terracotta 3.1

Ari Zilka, CTO of Terracotta, Inc., talks about the new features in Terracotta 3.1, announced during JavaOne and available now. (June 15, Tech Talk)

Enterprise Application Integration, and Spring

In this Tech Talk, Josh Long explores an integration challenge using Spring Integration and walks through the implementation, employing and expanding on the basic patterns of Enterprise Application Integration to tie together components into a function integration solution, and then demonstrates how Spring Integration helps address the integration requirements. (June 15, Tech Talk)

Google Web Toolkit: An Introduction

In this Tech Talk, David Geary teaches you: The basics of Google Web Toolkit; How to implement Ajax-enabled applications in Java; Internationalization; Hooking into the browser history mechanism; Remote procedure calls. (June 4, Tech Talk)

Just Enough Early Architecture to Guide Development

Jon Kern discusses the best architecture/technical solutions and ensure that they are repeated by all developers. By tackling the architecture up-front in a serial manner, subsequent parallel development will be much more manageable and predictable. (May 28, Tech Talk)

Productive Programmer: On the Lam from the Furniture Police

This keynote describes the frustrations of modern knowledge workers in their quest to actually get some work done, and solutions for how to guard yourself against all those distractions. Neal Ford talks about environments, coding, acceleration, automation, and avoiding repetition as ways to defeat the misguided attempts to sap your ability to produce good work. (May 26, Tech Talk)

Auto-Scaling Your Existing Web Application

Gil demonstrates how new, aggressive uses of already abundant compute capacity by common applications offer competitive value for application designers. (May 21, Tech Talk)

Automating Hibernate Mapping and Queries For Java Web Development

Chris Keene introduces WaveMaker as a new way to automate the ability to generate Hibernate classes in order to more quickly bring OR mapping into an application. (May 19, Article)

Auto-Scaling Your Existing Web Application

In this session Nati Shalom demonstrates how to take a standard Java EE web application and scale it out or down dynamically without changes to the application code. Seeing as most web applications are over-provisioned to meet infrequent peak loads, this is a dramatic change because it enables growing your application as needed, when needed, without paying for unutilized resources. (May 19, Tech Talk)

Free Book PDF Download: Mastering EJB Third Edition

Mastering EJB was one of the original and most influential EJB books in the industry. Mastering EJB III now returns with two new expert co-authors, updated for EJB 2.1 and 30% new chapters including security, integration, best practices, open source, and more.
(Book PDF Download)

Application Server Matrix

The Application Server Matrix is a detailed listing of J2EE vendors and their application server products, with information on latest version numbers, J2EE spec support and licensing, pricing, platform support, and links to product downloads and reviews.
(Application Server Comparison Matrix)

News | Blogs | Discussions | Tech talks | Patterns | Reviews | White Papers | Downloads | Articles | Media kit | About
Java Solutions
All Content Copyright ©2007 TheServerSide Privacy Policy
Site Map