java, initialize SubClass from SuperClass

Rami.Q :
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

Ted Hopp :

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);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java subclass initialize with arguments of superclass

From Dev

Java:Method called from subclass in superclass constructor

From Java

Calling superclass from a subclass constructor in Java

From Dev

New to Java: SuperClass and SubClass

From Java

Java, is it possible to 'convert' object from subclass to object from superclass

From Java

Initialize superclass variables (needed in constructor) in a subclass

From Dev

How to initialize a subclass when no superclass init fits

From Java

Java create Iterable<? extends SuperClass> from Iterator<SubClass> without warning

From Java

Java cast from subclass to superclass under different classloader

From Dev

How do you call a subclass method from a superclass in Java?

From Java

java subclass: multiple constructors inherited from abstract superclass

From Dev

Exception java.lang.classCastException when cast subclass from SuperClass

From Java

java - Calling a subclass method from dynamically casted superclass

From Dev

Superclass overriding Subclass with default values from constructor in Java

From Dev

Calling a subclass method from an ArrayList of type superclass in Java

From Java

Is there any way to initialize member variables of a subclass in Java before the superclass' constructor is called?

From Dev

Getting name of subclass from superclass?

From Java

Calling a subclass method from superclass

From Dev

instantiate subclass from existing superclass

From Dev

Change property of superclass from subclass

From Dev

Calling superclass methods from a subclass

From Dev

Java superclass catch and subclass catch

From Dev

If I use Superclass to initialize a subclass object, why will the object have the attributes of the subclass and but the methods of the superclass?

From Java

Why is "this" from superclass calling method from subclass?

From Dev

Correct way to see if the current instance of a superclass is a subclass from within the superclass

From Java

Merging @Sql from superclass with @Sql in subclass

From Java

Parameterizing superclass with static member class from subclass

From Dev

Calling constructor of subclass from constructor of superclass

From Dev

Access Django SubClass Method from SuperClass

Related Related

  1. 1

    Java subclass initialize with arguments of superclass

  2. 2

    Java:Method called from subclass in superclass constructor

  3. 3

    Calling superclass from a subclass constructor in Java

  4. 4

    New to Java: SuperClass and SubClass

  5. 5

    Java, is it possible to 'convert' object from subclass to object from superclass

  6. 6

    Initialize superclass variables (needed in constructor) in a subclass

  7. 7

    How to initialize a subclass when no superclass init fits

  8. 8

    Java create Iterable<? extends SuperClass> from Iterator<SubClass> without warning

  9. 9

    Java cast from subclass to superclass under different classloader

  10. 10

    How do you call a subclass method from a superclass in Java?

  11. 11

    java subclass: multiple constructors inherited from abstract superclass

  12. 12

    Exception java.lang.classCastException when cast subclass from SuperClass

  13. 13

    java - Calling a subclass method from dynamically casted superclass

  14. 14

    Superclass overriding Subclass with default values from constructor in Java

  15. 15

    Calling a subclass method from an ArrayList of type superclass in Java

  16. 16

    Is there any way to initialize member variables of a subclass in Java before the superclass' constructor is called?

  17. 17

    Getting name of subclass from superclass?

  18. 18

    Calling a subclass method from superclass

  19. 19

    instantiate subclass from existing superclass

  20. 20

    Change property of superclass from subclass

  21. 21

    Calling superclass methods from a subclass

  22. 22

    Java superclass catch and subclass catch

  23. 23

    If I use Superclass to initialize a subclass object, why will the object have the attributes of the subclass and but the methods of the superclass?

  24. 24

    Why is "this" from superclass calling method from subclass?

  25. 25

    Correct way to see if the current instance of a superclass is a subclass from within the superclass

  26. 26

    Merging @Sql from superclass with @Sql in subclass

  27. 27

    Parameterizing superclass with static member class from subclass

  28. 28

    Calling constructor of subclass from constructor of superclass

  29. 29

    Access Django SubClass Method from SuperClass

HotTag

Archive