Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Is my understanding of explicit casting correct?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Is my understanding of explicit casting correct?

    For the below code:
    public interface Printable {
        void print();
    }
     
     class ShoppingItem {
        public void description() {
            System.out.println("Shopping Item");
        }
    }
     
     class Book extends ShoppingItem implements Printable {
        public void description() {
            System.out.println("Book");
        }
     
        public void print() {
            System.out.println("Printing book");
        }
    }
    }
    Why is it that this fails:
    class MyClass {
        public static void main(String args[]) {
            Printable printable = new Book();//Implicit cast
            printable.description(); //This fails
     
        }
    And this succeeds?:
    class MyClass {
        public static void main(String args[]) {
            Printable printable = new Book();//Implicit cast
            ((Book)printable).description(); //Explicit cast - This works
     
        }

    I had initially understood that the reference could only access members of the object that it shared. Implicit casting is what makes this possible by cause I can assign a sub-class to a superclass reference .
    Explicit casting (in this example that I'm reading) seems to indicate that explicit casting gives a base-class reference type access to the finer implementation of the subclassed object.
    Is this correct? It seems that implicit and explicit casting are complimentary and don't overlap at all.


  2. #2
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is my understanding of explicit casting correct?

    first printable.descirption() fails, as compiler will complain that it does not know any method called description() in Printable interface.

    In second case as you have casted to Book interface, so there is no issue with compiler. and as real object too is book, so no issue at run time as well.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is my understanding of explicit casting correct?

    Thanks Deepak, I understand now.

Similar Threads

  1. About casting
    By CheukKwan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 18th, 2013, 11:03 AM
  2. no explicit pointer in Java..
    By Arnab Kundu in forum Object Oriented Programming
    Replies: 1
    Last Post: July 28th, 2013, 11:04 PM
  3. Explicit Casting?
    By syregnar86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 14th, 2013, 06:44 PM
  4. Casting Theory
    By BigDru in forum Java Theory & Questions
    Replies: 1
    Last Post: October 25th, 2012, 08:21 AM
  5. Explicit Conversion?
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 11th, 2009, 11:40 PM

Tags for this Thread