Results 1 to 6 of 6
  1. #1
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Method Casting help...

    I am trying to write a code that calls the method from the last class in itself to print out the letter A. This can only be done through casting, but unfortunately I am having trouble grasping the concept. I have read up on it on the internet but apparently I still don't understand the concept. Here is what I have

    Java Code:
    public static void main(String[] args) 
    	{
    		new Worker.doIt();
    	}
    	
    	class Worker 
    	{
    		void doIt()
    		{
    			base myVar=new A();
    			(()myVar)test();
    		}
    	}
    	
    	class base
    	{
    		
    	}
    	
    		class A extends base
    		{
    			public void test()
    			{
    			System.out.println("A");
    			}
    		}
    }
    It says I need to create a class doIt but I don't think I need to do that. I think there is another solution. Also it is not picking up my test method from the A class. Any help would be greatly appreciated!
    Last edited by Norm; 03-07-2012 at 01:28 AM. Reason: corrected code tags

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

    Default Re: Method Casting help...

    You have posted bits and pieces of your program. No way to tell what the code does or what you intend for it to do.
    Post code that will compile and execute.
    If you get compiler errors, post them.
    If you get execution errors, post them.

    If it executes, show the output and explain what is wrong with it and show what it should be.

  3. #3
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Method Casting help...

    Quote Originally Posted by Norm View Post
    You have posted bits and pieces of your program. No way to tell what the code does or what you intend for it to do.
    Post code that will compile and execute.
    If you get compiler errors, post them.
    If you get execution errors, post them.

    If it executes, show the output and explain what is wrong with it and show what it should be.
    the goal is to cast the method in the A class to the original and have it print out A as stated in the OP. The compiler displays

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Worker.doIt cannot be resolved to a type

    at cw0306a.main(cw0306a.java:6)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Method Casting help...

    the goal is to cast the method
    What do you mean by "cast the method"?
    Do you mean to cast the value that is returned by a method?

    Unresolved compilation problem:
    This error from your IDE doesn't say what the error is.
    Can you compile the code with the javac compiler and get an error message?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Method Casting help...

    In Java you cast variables and other expressions, you don't cast methods. Perhaps it would help if you posted the actual question. Don't worry: nobody will spoonfeed you a solution. But it may shed some light on what you are being asked to do.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Method Casting help...

    Quote Originally Posted by DMarsh12 View Post
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Worker.doIt cannot be resolved to a type

    at cw0306a.main(cw0306a.java:6)
    Java Code:
    new Worker.doIt();
    When instantiating (ie making) an instance of a class the call looks like:
    Java Code:
    new SomeClass();
    If I want to instantly execute a method on that instance (as you are trying to do with Worker):
    Java Code:
    new SomeClass().someMethod();
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. casting an int
    By droidus in forum New To Java
    Replies: 1
    Last Post: 09-07-2011, 06:38 PM
  2. Casting int[][] to int[]
    By subith86 in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 10:48 AM
  3. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 07:59 PM
  4. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 04:31 PM
  5. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-13-2009, 11:07 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
  •