4

My problem is that I've got 2 class: parent and child.

Parent class is abstract and child extends from them.

Then I've got a method that return a parent ArrayList and I need cast it to ArrayList of child.

What I should do?

2
  • 4
    Would you please add your code? that will be better for solving your issue Commented Mar 2, 2016 at 11:42
  • @miaguicam stackoverflow.com/questions/5763750/… I hope this will solve this issue. Commented Mar 2, 2016 at 11:50

2 Answers 2

6

You could do by the following way:

import java.util.ArrayList; import java.util.List; abstract class Parent { void callMe(){ System.out.println("Parent"); } } class Child extends Parent { void callMe(){ System.out.println("Child"); } } public class TestClass { public static void main(String[] args) { List<Parent> alist=new ArrayList<Parent>(); List<? super Child> alist2=alist; } } 

List<Parent> is not same as List<Child>. Compilor does not allow to assign the reference of List<Parent> to List<Child> even though List contains only Child Object.

For Example:

List<Parent> parentList=new ArryList<Parent>(); parentList.add(new Child()); parentList.add(new Child()); parentList.add(new Child()); //This is not allowed List<Child> childList=(List<Child>)parentList;//Compiler Error //But,This is allowed List<? super Child> childList=parentList; //Okey 

This is allowed becuase using reference of List<? super Child> gurantees that the List<Parent> will not be corrupted.

Sign up to request clarification or add additional context in comments.

Comments

0

Credit goes to Blackcompe

If you are using the generic version of the list implementation, you don't need to cast, e.g.

ArrayList<BestTutor> list = null; BestTutor c = list.get(0); 

Generics is a type-safety method that tells Java nothing other than a BestTutor will go into this collection, so you can always bet that List.get() will return a BestTutor or whatever the bounded object is. BestTutor is called the bounded object. If you don't use generics the bounded object is Object., e.g.

ArrayList<Object> list; 

Although, this bounding is implicit, so it's just:

ArrayList list; 

Java will check to see if computeArea has been overridden. If it has it will use that version, else it will use the inherited version. e.g.

class Parent { void callMe(){ System.out.println("Parent"); } } class Child { void callMe(){ System.out.println("Child"); } } Child c = new Child(); c.callMe(); //will display Child 

It would call the Parent version, which would print Parent, but we overrode the method. That's basic overriding. Java also has polymorphism:

Parent p = new Child(); p.callMe(); //will display Child 

A reference of type Parent can refer to an instance of Child.

If you call a method of Parent that's been overridden by Child Java knows to call the Child's instance method, not the Parent's.

A little advanced, but that will really become useful in more advanced design methods, like "coding to interfaces".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.