• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

object slicing .. is it possible in Java?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it ever possible to convert a derieved class to a base class (slice it)?
What I want to acheive is, by calling an overridden method on a variable, I want to be able to call the base class method.

Ex:

Basically, I create an object of type der. I want to convert the instance (runtime type) to bas.

'appreciate any help.
[ July 24, 2005: Message edited by: Mark Spritzler ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, not possible. der could have a method baseF1() that called "super.f1()", but unless der does this for you, there's no way to do it otherwise. This is part of Java's security/verification model.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of course it's possible...please mention me if i am wrong


Thank You
[ July 24, 2005: Message edited by: Ramakrishna Nalla ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramakrishna Nalla:
of course it's possible...please mention me if i am wrong




In your example you are
not calling the method f1() on an instance of the derived class (der). You are calling f1() on an instance of the base class (bas).

Read the original question again, carefully.
[ July 24, 2005: Message edited by: Barry Gaunt ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a little explaination that might help, taken loosly (I can't spell this word) from Kathy and Bert's SCJP book.

When your code compiles, the compile looks at the reference type with is Bas (Classes should start with a capital letter) and Bas has an f1 method, so it passes the compiler. But at runtime the JVM uses the actual instance type, which is Der, and calls the f1 method on Der.

In that case you cannot call the base classes f1 method. As has been stated.

Now try that in an array of Bas classes like

Bas[] bases = {new Bas(), new Der()};

and loop through them calling the f1 method.

Mark
 
Prasanta Chinara
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object slicing happens in C++ when objects are passed by value (NOTE: its objects and NOT references by value).

In Java everything is passed by ref and there is no pass by value semantics. So, in my opinion object slicing is not possible.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In Java everything is passed by ref and there is no pass by value semantics. So, in my opinion object slicing is not possible.


Not quite.
Everything in Java is passed by value.
I've said it before, and I can't be bothered saying it again.
...so I explained in here http://qa.jtiger.org/GetQAndA.action?qids=37
 
reply
    Bookmark Topic Watch Topic
  • New Topic