Results 1 to 4 of 4
  1. #1
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default How to do Upcasting of an Object ?

    Code -->


    class emp {

    emp() { }
    }

    class manager extends emp{

    manager() { }

    public static void main(String []a) {
    manager m=new manager();

    m=(manager)new emp(); // Run Time Class cast error


    if(m instanceof emp)
    System.out.println(" M is of Emp Class");
    else
    System.out.println(" M is of Manager Class");
    }
    }
    class upcasting extends manager {
    }


    Error -->

    java.lang.ClassCastException
    at manager.main(Empty.java:14)
    Exception in thread "main" 

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    The problem is that emp is not a manager. Not all employees are managers.
    It's the other way around -> manager is an emp. All managers are employees.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    you can't do this:
    Java Code:
    m=(manager)new emp();
    nor would you want to.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Casting is discussed on the page Inheritance.
    Java Code:
    class manager extends emp {
        manager() { }
    
        public static void main(String[] a) {
            manager m=new manager();
    
            System.out.println("m = " + m.getClass().getName());
            System.out.println("superclass of m = " +
                                m.getClass().getSuperclass().getName());
    
            emp anEmp = new emp();
    
            //manager misMatch = new emp();
    
            emp anotherManager = new manager();
    
            // This works but is not necessary.
            emp castUp = (emp)m;
    
            emp upCast = m;
            System.out.println("upCast = " + upCast.getClass().getName());
            System.out.printf("upCast instanceof manager = %b%n",
                               upCast instanceof manager);
            System.out.printf("upCast instanceof emp = %b%n",
                               upCast instanceof emp);
    
            if(m instanceof emp) 
                System.out.println(" M is of Emp Class");
            else 
                System.out.println(" M is of Manager Class");
        }
    }
    
    class emp {
        emp() { }
    }

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  3. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  4. Object
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 01-10-2008, 01:31 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 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
  •