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 4 of 4

Thread: Downcasting, most of the times it will fail but which is the pass case.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Happy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Downcasting, most of the times it will fail but which is the pass case.

    I have a class "Child" which extends class "Parent", and class Child doesn't even have a single function or variable.
    I created object for each and tried to cast parent object to child object. But I'm getting "java.lang.ClassCastException: deepJava.Downcasting cannot be cast to deepJava.ExtendedClass" Exception. I tried using "instanceOf" based on the suggestions from other blog and forums and its returning false. Technically there is no difference between Parent and Child object right? Why I'm getting false for "instanceOf".
    public class Parent
    {
     
    	public String testString;
    	int i = 8;
     
    	public void simpleFunction()
    	{
    		System.out.println(i);
    	}
     
    }
     
    class Child extends Parent
    {
    }
     
    class testDowncasting
    {
    	public static void main(String[] args)
    	{
    		Parent downCast = new Parent();
    		downCast.simpleFunction();
    		Child chClass = new Child();
    		chClass = (Child) downCast;
    	}
    }

    In my scenario, I don't have write access to the "Parent" class. Hench I need to play inside "Child" class only.
    And is there any workaround like calling each get methods from "Parent" class dynamically and set those in "Child" class dynamically?

    Thank you.

    --- Update ---

    Soon after posting this question, I tried the below code and it's working.

    Parent downCast = new Child();
    		downCast.simpleFunction();
     
    Child chClass = new Child();
    		chClass = (Child) downCast;
    		chClass.simpleFunction();

    But its not working if I set "parent" variables and assign that to "downCast", which is the crucial part, and later try to cast to Child.

    Parent downCast = new Child();
    Parent parentObj = new Parent();
    		parentObj.setTestString("Test 1");
                    downCast = parentObj;
     
    		Child chClass = new Child();
    		chClass = (Child) downCast;
    		System.out.println(chClass.getTestString());

    Is there any work around for "Downcasting"?

    Thank you.
    Last edited by Padmahasa; November 20th, 2017 at 07:51 AM.

  2. #2
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Downcasting, most of the times it will fail but which is the pass case.

    What would be the object of a puppy trying to bite or chase a human being down an old road, with the human being running for his life? Looking at the child class.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Happy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Downcasting, most of the times it will fail but which is the pass case.

    Lol. Don't know how to interpret your answer.
    Any other answer guys?

  4. #4
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Downcasting, most of the times it will fail but which is the pass case.

    This looks like a good structure. All methods of child class is from a parent class. Referent object child is equal to parent object referent. There is a type mismatch in the methods called. It should override parent when using the specific method. private methods or protected must be able to be called upon by a specific type class or instance class of that object referent. You might have to consider now which package you were interested in. import java.lang.Object; for example.

Similar Threads

  1. Downcasting/Inheritance
    By john2454 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 23rd, 2013, 09:59 PM
  2. Arraylist pass/fail values problem/confused.
    By BITmixit in forum Collections and Generics
    Replies: 3
    Last Post: February 25th, 2012, 09:58 AM
  3. Connect Four FAIL....
    By melinko928 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 14th, 2011, 02:10 PM
  4. Is Downcasting possible in Java??
    By tcstcs in forum Java Theory & Questions
    Replies: 8
    Last Post: April 7th, 2011, 04:55 AM
  5. 'pass by value' and 'pass by reference'
    By pokuri in forum Object Oriented Programming
    Replies: 5
    Last Post: January 26th, 2011, 11:30 AM

Tags for this Thread