|
| |||||||||
| Tags: class, functions, instantiating an object, members, methods, variables |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
}
} Code: Public class classfl1 extends mclass{
Public String getName(){
/ / your processing
}
} Code: Public void cr (mclass obj){
System.out.System.out.println(obj.getName());
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 02: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 04: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 02:13 PM |