-1

TLDR: I get an error when casting. I have commented the line.

I am trying to write this below program. My thoughts are as follows. I have a parent who has some debt, say a mortgage. this parent has a child who has a different form of debt (school fees.)

When I create a child, it is created with a debt object associated with it. However, I want to change the debt object to reflect the correct kind of debt the child has.

I thought that since I have made the casting explicit, I shouldn't have any issues with it. Can you explain why I am getting an error?

I have tried debugging and googling but I haven't had much luck other than needing to understand I need to explicitly cast.

public static void main(String[] args) {
    Child child = new Child("Tim");
}

public class Parent {
   protected String name;
   protected Debt debt;
     public Parent (String name){
       this. name= name;
       this.debt= new Debt();
     }
 }

public class Child extends Parent{
     protected SchoolFees schoolFees;
     public Child(String name) {
       super(name);
       schoolFees= new SchoolFees();
       schoolFees= (SchoolFees) debt;//when I comment this line out, the program runs fine. 
  }
}

public class Debt {}

public class SchoolFees  extends Debt{}

I have looked at the other stack over flow posts and see this:

class A {...}
class B extends A {...}
class C extends A {...}

**You can cast any of these things to Object, because all Java classes inherit from Object. You can cast either B or C to A, because they're both "kinds of" A. You can cast a reference to an A object to B only if the real object is a B. **

I think I am casting from B to A, which should be allowed. Let me know where I have gone wrong here.

ANSWER

You can't cast from a parent class to child class as they are not of the same type. In java, you can only cast objects of the type.

aroy
  • 23
  • 3
  • here you are trying to cast parent Object into child object. this will not work because maybe the child class have more methods that the parent class don't have so you will get `ClassCastException`. if you tried the other way road (casting `SchoolFees` into `Debt` it'll work. – odaiwa Oct 08 '22 at 11:24
  • @odaiwa Thanks for getting back. I am still confused, though. So why/ when do we do explicit casting ? Thanks for your help. – aroy Oct 08 '22 at 11:30

1 Answers1

0

The class Debt is not a subclass of SchoolFees, so you can't cast the variable debt of type Debt into a variable of type SchoolFees, if you do, a ClassCastException will be thrown.

In your example, you are casting class A (Dept) to class B (SchoolFees) – which doesn't work – and not the other way around.

Pexers
  • 953
  • 1
  • 7
  • 20
  • Sorry , it not clear to me. I saw another stack over flow post. See below. class A {...} class B extends A {...} class C extends A {...} You can cast any of these things to Object, because all Java classes inherit from Object. You can cast either B or C to A, because they're both "kinds of" A You can cast a reference to an A object to B only if the real object is a B. You can't cast a B to a C even though they're both A's. It can I can cast B to A, which is what I am doing here. Am I misunderstanding something? – aroy Oct 08 '22 at 11:35
  • No, you are not, in your example, you are casting class A (`Dept`) to class B (`SchoolFees`), if you were to cast class B to A, it would work no problem. I have updated my answer to include this information as well. If you found my answer helpful, please consider marking it as 'accepted', thank you! :) – Pexers Oct 08 '22 at 12:23
  • thanks, but still not clear. B to A is just implicit casting. you are right in that i am casting A to B. Hence, I have done explicit casting. I think I am allowed to do this. As per the other stack over flow post, "You can cast a reference to an A object to B only if the real object is a B". So what am i missing here? Thanks your time with this. – aroy Oct 08 '22 at 12:35
  • Explicit casting doesn't mean it won't throw an error. What you need to understand here is that class A can't be B because it is a parent class, but B can be of type A, because B is a child of A. If it's clear for you now, please consider marking my answer as 'accepted', thank you! – Pexers Oct 08 '22 at 12:46