Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java, initialize SubClass from SuperClass

public class Base {
  //long list of attributes
  // no Constructor using fields
  // no init methode 
  // i cannot change this class
}

now i extended the Base Class like:

public class subClass extends Base{
   private boolean selected;

   ...
   getter und setter
   ...
}

i become a list of Base object List<Base>

but i need the same list but as List<SubClass> is there a way to initialize the Subclass from the Base Class?

example:

for(Base b: list){
   SubClass sub = (SubClass)b; // Thats wrong i know
   if(...){
       sub.setSelected(true);
   }
   newList.add(sub);
}

i try to avoid the manual init of each Attribute of the Base Class to the SubClass

i update my Question as requested in the Comments: the Design above is just an example. my QUESTIN EXACTLY IS:

why converting BaseClass into SubClass (sence Subclass extends BaseClass) is not Possible? why Java dosn't allow me to do the following:

example:

Class Base{
   private String name;
.....
}
Class SubClass extends Base{
   private String title;
}

then

Base b = DBController.getById(...);
SubClass sub = (SubClass)b; 

after that the Object sub should have the Attribute Name from the Object b and the title Attribute is null

why is this not the case in java?

sorry for my bad english, thanks

like image 733
Rami.Q Avatar asked Apr 04 '13 04:04

Rami.Q


People also ask

Can a super class be instantiated as a subclass?

Because nobody other than the superclass itself can call its instance constructor, the subclass cannot be instantiated.

Can we call subclass constructor from superclass constructor?

A subclass can call a constructor defined by its superclass by use of the following form of super: super(parameter-list); Here, parameter-list specifies any parameters needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass constructor.

Can you cast a superclass to a subclass?

You can always successfully cast a superclass to a subclass. An interface can be a separate unit and can be compiled into a bytecode file. The order in which modifiers appear before a class or a method is important. Every class has a toString() method and an equals() method.

How do you declare a subclass in Java?

A class in Java can be declared as a subclass of another class using the extends keyword. A subclass inherits variables and methods from its superclass and can use them as if they were declared within the subclass itself: class Animal { float weight ; ... void eat () { ... } ... }


2 Answers

There is a way: Various Java Beans spec based manipulation.

For example:

Commons BeanUtils

for( Base base: list ){
   SubClass sub = new SubClass();
   PropertyUtilsBean.copyProperties( sub, base );
   if(...){
       sub.setSelected(true);
   }
   newList.add(sub);
}

This works based on get/setters of the same name. Doesn't copy internal fields.

If you needed to copy internal fields, it's actually not that hard to implement using javax.lang.reflect.

like image 114
Ondra Žižka Avatar answered Sep 26 '22 15:09

Ondra Žižka


If you have a List<Base>, then you cannot convert it to a List<SubClass>. This is mainly because the list may not contain instances of SubClass. The best you can do is:

List<SubClass> newList = new List<SubClass>();
for(Base b: list){
    if (b instanceof SubClass) {
        SubClass sub = (SubClass)b;
        . . .
        newList.add(sub);
    }
}

Generally, however, when you find yourself doing this kind of thing, there's something wrong with your design. You might want to avoid subclassing Base and using composition instead.

EDIT Based on your comments, it sounds like you want to construct a list of SubClass instances using a list of Base instances as a start. One approach is to define a constructor for SubClass that takes a Base as an argument.

public class SubClass extends Base{
    private boolean selected;

    public SubClass() {
        // default constructor
    }

    public SubClass(Base original) {
        // copy constructor -- initialize some fields from
        // values in original, others with default values
    }
    ...
    getter und setter
    ...
}

Then you can construct your new list with:

List<SubClass> newList = new List<SubClass>();
for(Base b: list){
    SubClass sub = new SubClass(b);
    . . .
    newList.add(sub);
}
like image 23
Ted Hopp Avatar answered Sep 23 '22 15:09

Ted Hopp