I have a SuperClass Employee and a Subclass Manager extends Employee with tribute Name.
Now I want to use instanceof while I go over an array of Employee
Example:
while (employee[i]!=null){
if (employee[i] instanceof Manager)
here is my problem"!!
I want to sysout a Manager atribute "Name":
sysout("Name: "+employee[i].name)
but it says create name in Employee.. why if it extends employee and already used instanceof...I tried Casting like this (Manager)employee[i].name but it doesn't do anything.
The cast needs to be applied to the value on which you are accessing the field.
((Manager)employee[i]).name
You were using it like
(Manager)employee[i].name
which attempted to apply the cast to the value returned by accessing the field name.
Related
ABTeacher and ABStudent is child class of ABUser
And in applyStudentChange I have Collection<ABStudent> ABStudents
I want to call changeABUser(Collection<ABUser> ABUsers) in applyStudentChange but the collection I have got is a collection of child class, how should I call changeABUser(Collection<ABUser> ABUsers) ?
I tried call this waychangeABUser((Collection<ABUser>) ABStudents), and this cast does not seem to work...
You need to give the following member type for your collection:
.... changeABUser(Collection<? extends ABUser> users) ....
I have class canonical name like this dev.ashish.mvc.beans.Employee.
Using this how can i create Class Employee at runtime in order access data members and member functions of Employee.
At runtime i want create class using its canonical name. At times it can be any entity Employee,Customer,User etc.
I tried this :
Class entityClass = Class.forName("dev.ashish.mvc.beans.Employee");
the above code does return class if i do entityClass.getName() it does return me dev.ashish.mvc.beans.Employee but how can i access methods of class Employee .
If i use java reflection like below :
Field field [] = entityClass.getClass().getDeclaredFields();
it returns me declared fields of class java.lang.Class instead of dev.ashish.mvc.beans.Employee
How can i achieve this ???
You already have your class in entityClass, so calling entityClass.getClass() will give you java.lang.Class and entityClass.getClass().getDeclaredFields() will indeed give you methods of Class not of your particular class.
You need:
Field field [] = entityClass.getDeclaredFields();
When you did:
Class entityClass = Class.forName("dev.ashish.mvc.beans.Employee");
you just got class of Employee. Now you have to create instance of it:
Employee employee = (Employee) entityClass.newInstance();
Update:
My answer is wrong. I though you need to work with methods of instance. But you need to access class methods.
You can call a method by name thus (adding to what has already said by others)
Method methodToCall = cls.getMethod("method_name");//cls is of type Class (e.g Employee.class)
methodToCall.invoke(obj, args);//obj is of type Employee in your case
Note that cls is of type Class and you already have to have an instance of the class that you want to call a method on. Note that even if you're calling a method without arguments you still have to pass a class instance (obj in this example), which means that the first argument is always a class instance.
I'm having a problem with casting in Android.
I'm developing an App that handle multiple devices, and i'm trying to make a dynamic class alocation (i.e, User sets the device and the app instanciate the class according to the user settings)
Here is a Sample code:
String Usr_imput; //name of the class
Class class = Class.forName(Usr_Input);
Object o = class.newInstance();
with that I can't access methods from the Usr_Input Class. The method class.cast(o) should be the solution to my problems but I can't get it to work, does the cast statement stacks?
Isn't it suposed to work if I use:
class.cast(o);
o.Method();
Anyone has experience on that?
Usr_Input o = (Usr_Input)class.newInstance();
from java doc:
cast
public T cast(Object obj)
blahblah..
Returns:
the object after casting, or null if obj is null
From your codes, you didn't catch the return value. class is not a good name either. check the comment above.
class.cast(o);
o.Method();
I want to access the Class Type name in order to specify the runtime type. For example
DAO<?> i = DAO<?> DOA.class.forName(”Student”).newInstance();
What I am trying to do is I have a class called Student and at runtime I want to specify DAO. When running the code I get a ClassCastException.
Is there any way of getting the Student type instead of String so that the following can be done
DAO<?> = new DAO<Student>();
by specifying ?
I'm writing a servlet-filter as the solution of this question:
Is it a good idea to filter inside a JSF template?
now, the idea is to create a big filter to check all privilegies and give the access or not to a certain user.
I create a Map to contains all privilegies for all sub applications and it has the sub application's id (a Long value) as Key and for the value another Map that contains other important informations.
The controller classes are named class1Controller, class2Controller ecc and the subapplications are stored in many folder named class1, class2 ecc...
The last thing that I must say is that all classes have a parameter called applicationID that is the same key of the Map that I mentioned previously.
So, what I would do?
I can retrieve the subapplication visited by the user using getRequestURI() method from HttpServletRequest, the problem is that I should take the application id from the class linked to that application, so I wrote this code:
Long id= ((Class.forName(packageName+applicationName+"Controller"))session.getAttribute(applicationName+"Controller")).getApplicationId();
The problem is that the compiler returns that it can't find method getApplicationId()!
Can I do something to resolve this problem? Or I must find another way to do that?
The last thing that I must say is that all classes have a parameter called applicationID
It sounds like you want an interface with the getApplicationId method in; make all the controllers implement that interface, and then all you need to do is cast to that interface.
// TODO: Work out a better interface name than "Application" :)
Object attribute = session.getAttribute(applicationName+"Controller");
Long id = ((Application) attribute).getApplicationId();
(You might want to use an abstract base class as described by BalusC - they're variations on the same theme, really.)
You're calling getApplicationId() on a Class instance, but it does not have that method at all.
The normal approach is to let all those classes extend some common base abstract class or an interface which has the method definied and then cast to that base abstract class or interface instead.
E.g. with a base abstract class:
public class FooController extends BaseController {}
public class BarController extends BaseController {}
etc..
Where the base abstract class look like this:
public abstract class BaseController {
public Long getApplicationId() {
return applicationId;
}
}
Then you can get it as follows:
Long id = ((BaseController) session.getAttribute(applicationName + "Controller")).getApplicationId();