Using Object class Reference variable, accessing different class members. - java

I am trying to access class members of different class i.e.,getDetails() from student as well as customer class using Object class Reference variable. But it looks like its not working. Please look into this easy code and help me out how to access the getDetails() using Object class ob[0] and ob[1]
class Customer
{
int custId;
String name;
Customer(String name, int custId)
{
this.custId = custId;
this.name = name;
}
public void getDetails()
{
System.out.println(this.custId+" : "+this.name);
}
}
class Student
{
int roll;
String name;
Student(String name, int roll)
{
this.name = name;
this.roll = roll;
}
public void getDetails()
{
System.out.println(this.roll+" : "+this.name);
}
public static void main(String []args)
{
Object[] ob = new Object[2];
ob[0] = new Student("Vishal", 041);
ob[1] = new Customer("Xyz" , 061);
ob[0].getDetails();
ob[1].getDetails();
}
}

Try creating a common interface that declares the method getDetails. Something like this:
public interface Person {
public void getDetails();
}
Let Student and Customer implement the interface. Then declare the array like this:
Person ent[] ob = new Person[2];
....

Related

How to invoke method on a specific definition of a class using reflection?

I am trying to pull a field via reflection that is an array of the below class.
package com.api.Person
public class Person {
private String name;
private int age;
public Person() {
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.client.Client
public class Client{
...
People[] peoples;
...
}
I know how to get the field I am looking for and declare my methods. So below, obj would be my People[] and methodgetAge and methodsetAge are two methods I have defined that act on a Person class. How do I take my obj and loop through it to get individual People and call methodgetAge on each person?
Class<?> mainClass = cl.loadClass("com.client.Client");
Class<?> peopleClass = cl.loadClass("com.api.Person");
Field allPersons = mainClass.getDeclaredField("peoples");
allPersons.setAccessible(true);
Object obj = allPersons.get(mainClass);
Method methodgetAge = peopleClass .getDeclaredMethod("getAge");
Method methodsetAge = peopleClass .getDeclaredMethod("setAge", int.class);

How to set / get a name to "ford" - instance of class Car I have created in main method of class Vehicle?

As you can see I am stuck in part where I should set a name an owner of ford... Thanks for help.
public class Vehicle {
Person owner;
long motorSerialNo;
String registerNo;
public static void main(String[] args) {
//an example, create an object instance of class Car
Car ford = new Car();
ford.model = "Focus";
ford.motorSerialNo = 123456;
ford.registerNo = "CA-126-65";
//and here is a problem
ford.owner.setName("John Croul");
}
}
class Car extends Vehicle {
String model;
}
class Person {
public Person(String name){
this.name = name;
}
String name;
String lastname;
String address;
String getName() {
return name;
}
void setName() {
this.name = name;
}
}
Firstly, your setter should look like
public void setName(String name) {
this.name = name;
}
Then you have to initialize the instance variable person before calling its method setName(), otherwise you will get the NullPoiterException.
Person owner = new Person();
or in the main method, as you did for other variables
ford.owner = new Person();

Get object's attribute into java for each loop

public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age= age;
}
Person Rome[] = new Person[10];
public void initializes {
Rome[0]= new Person ("Antonio",20);
Rome[1]= new Person ("Marco",11);
//...
Rome[9]= new Person("Giuseppe",27);
}
public void printName(){
for(Person x : Rome){
System.out.println(x.name);
}
}
//TEST CLASS
public static void main (String args[]){
Person obj = new Person();
obj.initializes();
obj.printName(); // Exception in thread "main" java.lang.NullPointerException
}
}
why the print of for each work just with primitive object , if i want print a attribute of complex object not work why?
You need to initialize Rome[0] object.
It will be look like:
public void initializes {
Rome[0]= new Person ("name",00);
Rome[1]= new Person ("Antonio",20);
//...
}
And code will be better if you'll initialize your object without object class. In test class for example:
Person[] Romeo = new Person[] {
new Person(name, 00),
//...
}
And the result:
the file name Test.java
//and there is the code. it is prototype and I did'n compile it, but it is an example for you
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age= age;
}
public void printName(){
System.out.println(name);
}
}
//Test class
public class Test {
public static void main (String args[]){
Person[] Romeo = new Person[] {
new Person("name", 00),
//...
}
for(int i = 0; i < 10; i++)//or another loop style
Romeo[i].printName();
}
}

Adding objects of a class to an ArrayList in another class

Im trying to add Objects of a Class called Student to an Arraylist in another class called StudentClass. I have initialised the list, but am having trouble working out how to add objects to the ArrayList
Code for StudentClass
public class StudentClass
{
List<Student> studentList;
private String studentName;
private int lengthOfString;
public StudentClass()
{
super();
studentList = new ArrayList<>();
}
public void addStudent(String aName)
{
String objt = new String(name, mark);
studentList.add(Student);
}
Code for Student
public class Student
{
private String name;
private int mark;
public Student(String aName)
{
super();
this.name = aName;
this.mark = -1;
Try this:
public void addStudent(String aName) {
studentList.add(new String(aName));
}
Also, Student class constructor does not accept marks as second parameter, at least the code what you have posted.
This should work.
import java.util.*;
public class StudentClass
{
List<Student> studentList;
private String studentName;
private int lengthOfString;
public StudentClass()
{
super();
studentList = new ArrayList<>();
}
public void addStudent(String aName) {
Student student = new Student(aName);
studentList.add(student);
}
}
public class Student
{
private String name;
private int mark;
public Student(String aName)
{
super();
this.name = aName;
this.mark = -1;
}
}
I'm not clear what you're trying to do with the mark?
Seems that your mistake is here:
String objt = new String(name, mark);
studentList.add(Student);
Write this instead:
Student objt = new Student(name, mark);
studentList.add(objt);
You need to create a new Student object and then add that to your list:
public void addStudent(String aName) {
Student student = new Student(aName);
studentList.add(student);
}
It's not really clear from your provided code how the mark field is set, but either pass it through the Student construction and/or add getters and setters.

How to accept Class return in reflection?

take the following as an example:
public class Person {
private String name;
private Achievements achievements;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Achievements getAchievments() {
return achievements;
}
}
and the achievements class:
public class Achievements {
public int amount() {
...
}
}
if i would be using the class regularly, i would go like :
Person p = new Person("John");
p.getAchievements().amount();
however, if i want to use the reflection API, how would i go about reaching to the amount(); step?
i know how to reach to the getAchievements();
however i'm blocked from the rest.
thanks
If I correctly understand you might need something like this:
Person obj = new Person("qew");
Method methodGetAchievements = obj.getClass().getMethod("getAchievments");
Object achievements = methodGetAchievements.invoke(obj);
Method methodAmount = achievements.getClass().getMethod("amount");
int amount = (int) methodAmount.invoke(achievements);

Categories

Resources