Results 1 to 6 of 6
  1. #1
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default upcast and override

    Java Code:
    class A{
        public void print(){
            System.out.println("A");
        }
    }
    
    class B extends A{
        public void print(){
            System.out.println("B");
        }
    }
    
    public class Test{
    
        B objectB = new B();
        A as = (A) objectB;
        as.print();
     
    }
    why the result is "B" instead of "A"?
    is
    A as = (A) objectB;
    the same as
    A as = objectB; ?
    Last edited by esolve; 09-21-2016 at 07:43 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: upcast and override

    Because the cast only affects the reference variable, it does not affect the underlying Object.
    So the reference 'as' is referring to an object of type B.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default Re: upcast and override

    Quote Originally Posted by Tolls View Post
    Because the cast only affects the reference variable, it does not affect the underlying Object.
    So the reference 'as' is referring to an object of type B.

    so if we want to affect the underlying object or in other words, if we want "as.print(); " print "A", what to do?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: upcast and override

    You can't without modifying class B to call class A's print method. Or create an instance of class A. BTW, your
    test class is incorrect as as.print() isn't allowed there.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: upcast and override

    What is the purpose of casting an object then? Is there any practicality to it?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: upcast and override

    Quote Originally Posted by camel-man View Post
    What is the purpose of casting an object then? Is there any practicality to it?
    Yes. It was probably more prevalent before generics but there are still plenty of times you need to. For example,
    when overriding the equals method. The argument to equals is of type Object. So all you have access to are the methods
    of type Object (the others are hidden). So to access the methods of the real type you need to cast it.

    The same is true for getting the source of events. In some cases the object is retrieved via getObject(). But you need to cast it
    to be able to access the methods of the actual class that generated the event.

    And a simpler example. What is you want to have a List that contains both Strings and Integers. You would have to do something like
    the following:

    Java Code:
          List<Object> list = new ArrayList<>();
          list.add(25);
          list.add("Hello");
          list.add("Universe");
          list.add(19);
          for (Object o : list) {
             if (o instanceof Integer) {
                int v = (Integer)o; // unboxed after cast
                System.out.println(v*23);
             } else if (o instanceof String) {
                String s = (String)o;
                System.out.println(s + " has length of " + s.length());
             }
          }
    Regards,
    Jim
    Last edited by jim829; 09-29-2016 at 07:17 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. OverRide
    By yanivdan98 in forum New To Java
    Replies: 5
    Last Post: 02-24-2013, 10:37 PM
  2. A question on @Override
    By fatabass in forum New To Java
    Replies: 9
    Last Post: 02-02-2012, 11:07 AM
  3. Cannot override PasteAction
    By madroadbiker in forum Advanced Java
    Replies: 8
    Last Post: 06-10-2011, 01:43 PM
  4. Is it necessary to override setValueAt
    By pink123 in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2011, 12:17 AM
  5. @Override Annotation
    By Unsub in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •