Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

convert base class to derived class

 
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.
 
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.
 
Ranch Hand
Posts: 2410
  • 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.
 
author
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic