Results 1 to 6 of 6

Thread: Instantiate object of a class

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Instantiate object of a class

    Hello,
    I have two methods, a main method that calls my second method. My main method is to effect treatment, regardless of class provides a parameter to the method , because the same treatment, only the instantiation of the object changes. The problem is that I do not know how, once in my method, how to instantiate an object of my class, while I provided in the method parameter type "Object". So, if any one has any idea of how to do it, then please let me know, thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Instantiate object of a class

    Hello,
    Is it normal for your method has no return type? Do you believe in passing an object of a certain type parameter of a method you can use it as a type and not as an object?
    Here is an example, have a look at it
    Code:
    static void cr(Object obj){
        Object obj = obj.getClass().newInstance();
    }

  3. #3
    Join Date
    Dec 2009
    Posts
    263

    Re: Instantiate object of a class

    Hello,
    I think even I have a similar kind of a problem, I have 2 subclasses, Class 1 mother and 1 for class I call here "Main" with my 2 methods.
    in my main class, main method: 1 object I instantiate a subclass, I pass a parameter to the method. I wish I could "see" the methods of my own class but I can not because of the different object type. I do not see any solution for this, please if you help me then it would be great. Thanks in advance.

  4. #4
    Join Date
    Dec 2009
    Posts
    178

    Re: Instantiate object of a class

    Hello,
    Here is my updated code, Have a look at it, if you think that the code is wrong somewhere then please guide me with the correct.
    Here is my updated code:
    Code:
    hand{
    
    myclass m = new myclass();
    codeRepet(m);
    
    my2class m2 = new my2class();
    codeRepet(m2)
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Instantiate object of a class

    Hello,
    I think you have done something wrong in your code, take a look at the following part of the sample code, I think this will help in writing you the proper code.
    Here is the snippet
    Code:
    Public void cr(ClassMere obj) {
        if (obj instanceof fl1) {
             ((fl1) obj).methodefl1();
        }
        if (obj instanceof fl2) {
             ((fl2) obj).methodefl2();
        }
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: Instantiate object of a class

    Hello,
    Just see how all it works, that is how you carry inheritance in your program.
    Code:
    Public class mclass{
    Public String getName(){
    / / your processing
    }
    }
    For your child classes:
    Code:
    Public class classfl1 extends mclass{
    Public String getName(){
    / / your processing
    }
    }
    and call in your method:
    Code:
    Public void cr (mclass obj){
    System.out.System.out.println(obj.getName());
    }
    Hope you have understood what I am trying to explain you here.

Similar Threads

  1. Replies: 6
    Last Post: 06-06-2011, 01:04 AM
  2. Instantiate different objects of same class
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 09-03-2010, 09:35 AM
  3. Replies: 3
    Last Post: 20-08-2009, 02:52 PM
  4. What is the Difference between Class and Object?
    By RupaliP in forum Software Development
    Replies: 5
    Last Post: 28-02-2009, 06:33 PM
  5. Class Object in C++
    By paintbox in forum Software Development
    Replies: 0
    Last Post: 25-10-2008, 12:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,638,713,549.56056 seconds with 17 queries