Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualThe King2

Posted 05 April 2013 - 01:09 PM

If you type "java downcast" in google, this is the first result:

 

http://stackoverflow.com/questions/380813/downcasting-in-java

 

In short, the "workaround goes as this:

 

public void doit(A a) {
    if(a instanceof B) {
        // needs to cast to B to access draw2 which isn't present in A
        // note that this is probably not a good OO-design, but that would
        // be out-of-scope for this discussion :)
        ((B)a).draw2();
    }
    a.draw();
}

(As a side note, you might want to get used to googling questions that don't need an extraordenary explanation or can be put in few words before you post here - even better, you might google or search the forum before anyway, thats going to be faster for you and reduce the amount of quadriplicated q&a out there drastically. if there is something unclear, you can ask afterwards anyway)


#2The King2

Posted 05 April 2013 - 01:09 PM

If you type "java downcast" in google, this is the first result:

 

http://stackoverflow.com/questions/380813/downcasting-in-java

 

In short, the "workaround goes as this:

 

public void doit(A a) {
    if(a instanceof B) {
        // needs to cast to B to access draw2 which isn't present in A
        // note that this is probably not a good OO-design, but that would
        // be out-of-scope for this discussion :)
        ((B)a).draw2();
    }
    a.draw();
}

(As a side note, you might want to get used to googling questions that don't need an extraordenary explanation or ca be put in few words before you post here - even better, you might google or search the forum before anyway, thats going to be faster for you and reduce the amount of quadriplicated q&a out there drastically. if there is something unclear, you can ask afterwards anyway)


#1The King2

Posted 05 April 2013 - 01:06 PM

If you type "java downcast" in google, this is the first result:

 

http://stackoverflow.com/questions/380813/downcasting-in-java

 

In short, the "workaround goes as this:

 

public void doit(A a) {
    if(a instanceof B) {
        // needs to cast to B to access draw2 which isn't present in A
        // note that this is probably not a good OO-design, but that would
        // be out-of-scope for this discussion :)
        ((B)a).draw2();
    }
    a.draw();
}

PARTNERS