Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a subclass from superclass

Let's say:

class A {
    public int fieldA = 0;
}

class B extends A {
    public int fieldB = 0; 
}

Now, I want to create a instance of B from A:

A a = new A();
B b = (B) new A();

This gives a classCastException. I can make a costructor in B that takes a A instance and copy fields, but it's clearly non pratical:

class B extends A {
    public int fieldB = 0; 

    B (A a){
        fieldA = a.fieldA;
    }
}

Thank you for your help

like image 838
DeliriumTremens Avatar asked Dec 16 '22 10:12

DeliriumTremens


2 Answers

Take the following example

public class Animal
{
  public void eat(){}
}
public class Dog extends Animal
{
  public void eat(){}
  public void main(String[] args)
  {
    Animal animal=new Animal();
    Dog dog=(Dog) animal; //will not work
  }
}

By using a cast you're essentially telling the compiler "trust me. I'm a professional, I know what I'm doing and I know that although you can't guarantee it, I'm telling you that this animal variable is definitely going to be a dog."

Since the animal isn't actually a dog (it's an animal, you could do Animal animal = new Dog(); and it'd be a dog) the VM throws an exception at runtime because you've violated that trust (you told the compiler everything would be ok and it's not!)

The compiler is a bit smarter than just blindly accepting everything, if you try and cast objects in different inheritence hierarchies (cast a Dog to a String for example) then the compiler will throw it back at you because it knows that could never possibly work.

Because you're essentially just stopping the compiler from complaining, every time you cast it's important to check that you won't cause a ClassCastException by using instanceof in an if statement (or something to that effect.)

In your case you are saying my reference b will point to object of point B and then you make it point to object of class A(which is not object of class B that reference b was expecting). Hence you get the classCastException.

You could do something like

A a = new B();
B b = (B) a;
like image 77
Aniket Thakur Avatar answered Dec 18 '22 00:12

Aniket Thakur


The use for Class cast is something different.

class Employee()
{
    //body
}

class SalariedEmployee extends Employee()
{
    //body
}

You have some polymorphic code.

public static void met(Employee e)
{
    SalariedEmployee s = (SalariedEmployee)e
}

public static void somemethod()
{
    //You write a polymorphic method
    //You will create a list of Employees
    //Then you can write
    ArrayList<Employee> employees = new ArrayList<Employee>()

   //Create a SalariedEmployee
   Employee e = (Employee) new SalariedEmployee()
   employees.add(e);

   e = (Employee) new CommissionEmployee()
   employees.add(e);         

}

You can't do SalariedEmployee b = (SalariedEmployee) new Employee(); because every Employee is not guaranteed to be a SalariedEmployee.

like image 21
Akshar Raaj Avatar answered Dec 18 '22 00:12

Akshar Raaj