It is possible to cast objects of abstract classes

4

hi how I have a question that is killing me I hope you can help me

what I know according to what I understand:

  

You can not instantiate objects of an abstract class

my doubt:

The Graphics2D class inherits from Graphics both are abstract so I can not instantiate objects directly from both. then I created a GraFi class that extends or inherits Graphics and then I want to cast it to Graphics2D but I have an error

error:

Cannot cast from GraFi to Graphics2D

I thought maybe it's because Graphics2D can not be transformed by the level of inheritance (maybe I'm wrong).

Now when I do this things change but I would like to know why:

As you can see there, the error disappears, but there is a doubt if, when creating an object of the GraFi class that I inherited from Graphics, it would not let me convert it to Graphics2D because now that I install a Grafi object to graphics Graphics belonging to Graphics and I do not get an error if in both cases you are trying to convert graphics to Graphics2D even if in the first case you think so

Grafi graficoDeGraphics = new Grafi(); 

if GraFi has belonged to GraFi

    
asked by Vladimir Joel 16.05.2018 в 09:39
source

2 answers

4

I think you have already answered only, but I put it in a more formal way.

The same thing you see more clearly with the classic example zoo

abstract class Animal {...}
abstract class Mamifero extends Animal {...}
abstract class Ave extends Animal {...}
class Aguila extends Ave {...}

As expected:

Aguila a= new Aguila()
if (a instanceof Ave) {
   ... //entra
}
if (a instanceof Animal) {
   ... //entra porque un Aguila es un Ave, que es un Animal
   Animal an = a; //no hace falta ni hacer casting, es implícito por ser seguro
}
if (a instanceof Mamifero) {
  ... // aqui no va a entrar
}

Animal an = new Aguila() //funciona

Aguila ag= (Aguila) an;
//funciona pero, como restringes, hace falta el casting explícito.
// (decir que es un Aguila es más restrictivo que decir un Animal)

The last assignment has demanded an explicit casting to force you to tell the compiler I know what I'm doing . That is, not all animals are eagles but I know that in this case it is like that and I can force it.

Mamifero m= (Mamifero) a;  //no te deja, en ningún caso un Aguila es un mamifero
    
answered by 16.05.2018 / 10:05
source
1

In the first example, you are casting two classes (sisters) that come from Graphics then, that is, both classes have common properties but they are not the same as casting, so it's wrong.

The second case leaves you because you are defining the Grafi class as a Graphics class and it lets you cast it because Graphics is a parent of Graphics2D.

In summary, the abstract classes can be inherited but you have problems to understand the inheritance take a look. My advice is ...

  

Always casts children to parents and never casts from parents to children, unless you are very sure that parent class contains the necessary properties to be able to cast the child correctly.

    
answered by 16.05.2018 в 10:14