Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert base class to derived class

 
Mark Lybarger
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in java i can't do something like:
BaseClass a = new BaseClass();

DerivedClass b = (DerivedClass) a;

is there some utility to _cast_ a base class to a derived class? I don't want to have to implement a clone method just to invoke the getters on the base, and the setters on the derived.

i have an ejb which returns some classes. another ejb takes those classes as arguments, but i need to extend them locally, and add some information about them.
 
Zip Ped
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a method inside the derived class which can call the setter of the Base class by calling super.<setterMethodName>() inside that method.
eg.

public class DerivedClass{
//......;

void methodName(pass parameter){
super.<setterMethodName>()
}
//......;

}

DerivedClass b = new DerivedClass();
b.methodName(parameter)


The base class can be cast to a derived class only if the BaseClass object is refering to a Derived class object at runtime.
eg.
DerivedClass b = new DerivedClass();

BaseClass a = b;

b = (DerivedClass)a;


If the BaseClass object refers to an object of the type BaseClass or any other class other than the DerivedClass it would give you a ClassCastException.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Lybarger:
in java i can't do something like:
BaseClass a = new BaseClass();

DerivedClass b = (DerivedClass) a;

is there some utility to _cast_ a base class to a derived class? I don't want to have to implement a clone method just to invoke the getters on the base, and the setters on the derived.

i have an ejb which returns some classes. another ejb takes those classes as arguments, but i need to extend them locally, and add some information about them.


Actually there is a good reason your code won't work. If an object reference is of type A, and B is a subclass of A, it is not necessarily true that the object is of type B. For example, a String is an Object, but an Object isn't necessarily a String.

Having the base class contain abstract getter and setter methods, and having the parameter type of the method be the base class would be the better way to go.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Lybarger:
is there some utility to _cast_ a base class to a derived class?


That's not possible, as the derived class could have additional fields - what values should those have?

What you should do is rethink your design. Perhaps the derived class shouldn't inherit the base class as all, but have a reference to an instance of it?

Can you tell us more about the design?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic