Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Instantiate object of a class

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 08-03-2010
Member
 
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.

Reply With Quote
  #2  
Old 08-03-2010
Member
 
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();
}
Reply With Quote
  #3  
Old 08-03-2010
Member
 
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.
Reply With Quote
  #4  
Old 08-03-2010
Member
 
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)
}
Reply With Quote
  #5  
Old 08-03-2010
Member
 
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();
    }
}
Reply With Quote
  #6  
Old 08-03-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Instantiate object of a class"
Thread Thread Starter Forum Replies Last Post
Getting HP AIO Device object server register class object failed message on my system Donoho Hardware Peripherals 6 06-06-2011 01:34 AM
Instantiate different objects of same class ISAIAH Software Development 5 09-03-2010 10:05 AM
fatal error: cannot instantiate non-existent class: imagick Broot Windows Software 3 20-08-2009 03:22 PM
What is the Difference between Class and Object? RupaliP Software Development 5 28-02-2009 07:03 PM
Class Object in C++ paintbox Software Development 0 25-10-2008 01:13 PM


All times are GMT +5.5. The time now is 09:53 PM.