Reputation: 93
In the Start Class I define two objects and pass information as arguments to these objects constructors I than create two instance variables in the Person Class and instantiate in the constrcutor the two variables why am I not able to call the getName()
method from class A
? How would I call the getName()
method from class A()
?
public class Start {
public static void main(String[] args) {
A objectA = new A("John");
B objectB = new B(24);
Person a = new Person(objectA, objectB);
a.getAge();
}
}
public class Person<A, B> {
A aa;
B bb;
Person(A aa, B bb) {
this.aa = aa;
this.bb = bb;
}
public void getAge() {
System.out.println(this.aa.getClass().getName());
System.out.println(this.bb.getClass().getName());
System.out.println(this.aa.getName());
}
}
public class A {
public String name;
public A(String name) {
this.name = name;
}
String getName() {
return this.name;
}
}
Upvotes: 0
Views: 122
Reputation: 143
It is because the Person class does not know about classes A
or B
. You have specified a generic type arguments for class Person<A, B>
- here A
and B
does not mean class-A or class-B, they are placeholders for other class types with the "variable-name" of A and B.
And when you say
A aa;
B bb;
in your class, it means that A and B will be replaced with the types you specify when creating an object of the class in triangular brackets. Since java is strongly typed language, it should have a guarantee that your code will work no matter what type you specify later. It fails, because it does not have any guarantee that any type you later specify as an argument - A will have getName() method. You can give a guarantee by saying that you are only allowed to pass type arguments which are subclasses of class-A:
public class Person {
T aa;
U bb;
Person(T aa, U bb) {
this.aa = aa;
this.bb = bb;
}
Upvotes: 1
Reputation: 820
class A {
public String name;
A(String name) {
this.name = name;
}
String getName() {
return this.name;
}
}
class B {
public int num;
public B(int a ) {
this.num = a;
}
int getNum() {
return this.num;
}
}
public class Start {
public static void main(String[] args) {
A objectA = new A("John");
B objectB = new B(24);
Person a = new Person(objectA, objectB);
a.getAge();
}
}
**class Person{**
A aa;
B bb;
Person(A aa, B bb) {
this.aa = aa;
this.bb = bb;
}
public void getAge() {
System.out.println(this.aa.getClass().getName());
System.out.println(this.bb.getClass().getName());
System.out.println(this.aa.getName());
}
}
No need to change access modifier of the method as you will be importing the class A .
Upvotes: 0
Reputation: 6276
the problem is that you have declared getName
function with default access i.e. package-private, just declare the function in class A
with public
modifier in case these class are in different packages as,
public String getName()
Upvotes: 1
Reputation: 896
You've created a generic class Person parameterized by two types A,B. These are just "placeholders" and have nothing in common with your classes A,B. Remove the types from the Person class and it should work. This means you have to define Person like that:
public class Person {
//the rest of you code here
}
BTW: you should get a compiler warning here that "type parameter A is hiding class A".
Upvotes: 3
Reputation: 12880
If the class Person
and Class A
are in different packages, you need to make your getName() method as public. It is currently with default access (package access). Thus, it is not visible and shows undefined.Change it like this
public String getName() {
return this.name;
}
Upvotes: 1
Reputation: 35547
You can simply do this
A a=new A();
String name=a.getName();
Upvotes: 1