Upcasting and Downcasting in java

后端 未结 8 1618
孤城傲影
孤城傲影 2021-01-13 21:26

I can understand what upcasting is but Downcasting is a little confusing. My question is why should we downcast? Can you help me with a real time example ? Is downcasting th

相关标签:
8条回答
  • 2021-01-13 21:44

    To access the header methods of of the ServletResponse in a filter, you have to downcast to to an HttpServletResponse.

    It's good to declare objects by the class they extend, so you can change the implementation on the fly. However if you need to access any of the methods that are specific to the implementation, you need to downcast.

    0 讨论(0)
  • 2021-01-13 21:46

    Downcasting Example

    //: RTTI.java
    // Downcasting & Run-Time Type
    // Identification (RTTI)
    import java.util.*;
    
    class Useful {
      public void f() {}
      public void g() {}
    }
    
    class MoreUseful extends Useful {
      public void f() {}
      public void g() {}
      public void u() {}
      public void v() {}
      public void w() {}
    }
    
    public class RTTI {
      public static void main(String[] args) {
        Useful[] x = {
          new Useful(),
          new MoreUseful()
        };
        x[0].f();
        x[1].g();
        // Compile-time: method not found in Useful:
        //! x[1].u();
        ((MoreUseful)x[1]).u(); // Downcast/RTTI
        ((MoreUseful)x[0]).u(); // Exception thrown
      }
    } ///:~ 
    

    Check source link for more information.

    0 讨论(0)
  • 2021-01-13 21:48

    Downcasting is needed when you receive an object that you know has some more specific (down the class hierarchy) type and you want to cast it to that type.

    Example: you receive Object from some service and you know that it is actually String and you downcast it to String. Before downcasting you should always check the type otherwise you risk the ClassCastException:

    Object receivedObject = receiveFromSomewhere();
    if(receivedObject instanceof String){
        receivedString = (String) receivedObject;
    }
    
    0 讨论(0)
  • 2021-01-13 21:51

    Downcasting is a necessary evil, for example when dealing with legacy APIs that return non-generic collections. Another classic example is an equals method:

    public class Phleem{
    
        public Phleem(final String phloom){
            if(phloom == null){
                throw new NullPointerException();
            }
            this.phloom = phloom;
        }
    
        private final String phloom;
    
        public String getPhloom(){
            return phloom;
        }
    
        @Override
        public boolean equals(final Object obj){
            if(obj instanceof Phleem){
                // downcast here
                final Phleem other = (Phleem) obj;
                return other.phloom.equals(phloom);
            }
            return false;
        }
    
        // ...
    
    }
    

    I can't think of an example where Upcasting is necessary though. OK, it's good practice to return the least specific possible Object from a method, but that can be done entirely without casting:

    public Collection<String> doStuff(){
        // no casting needed
        return new LinkedHashSet<String>();
    }
    
    0 讨论(0)
  • 2021-01-13 21:56

    What You need to remember is that downcasting is allowed when there is a possibility that it suceeds at run time.

    This will work:

    Object o = doStaff();
    String s = (String) o; 
    

    This will fail:

    Object o = new Object();
    String s = (String) s;
    

    Q1: Why we should use downcast ?

    It is generally up to developer, to used the downcast. Sometimes methods return Objects or use as parameter like equal method and then using subcast we can back to specific type.

    Q2: Is downcast important ?

    As everything in coding, but better word would be useful, IMHO it is.

    0 讨论(0)
  • 2021-01-13 21:59

    The best solution for your question is to read a good book. You will learn about polymorphism, objects, patterns ...
    Good start is "Beginning java objects 2nd edition"

    0 讨论(0)
提交回复
热议问题