How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?
up vote
0
down vote
favorite
How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?
I created a ArrayList of a superclass in another class. This superclass has three subclasses, each with their own methods. How do I use those methods?
And also, how can I use the instance variables of those subclasses?
java
add a comment |
up vote
0
down vote
favorite
How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?
I created a ArrayList of a superclass in another class. This superclass has three subclasses, each with their own methods. How do I use those methods?
And also, how can I use the instance variables of those subclasses?
java
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?
I created a ArrayList of a superclass in another class. This superclass has three subclasses, each with their own methods. How do I use those methods?
And also, how can I use the instance variables of those subclasses?
java
How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?
I created a ArrayList of a superclass in another class. This superclass has three subclasses, each with their own methods. How do I use those methods?
And also, how can I use the instance variables of those subclasses?
java
java
asked Nov 11 at 21:45
user10610048
676
676
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.
You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.
Here is an example:
public class A {
public void methodFromA() {
System.out.println("From class A");
}
}
public class B extends A {
public void methodFromB() {
System.out.println("From class B");
}
}
public class C extends A {
public void methodFromC() {
System.out.println("From class C");
}
}
and here are these classes in use, using an ArrayList with the superclass type:
public static void main(String args) {
List<A> myList = new ArrayList<>();
A object1 = new A();
B object2 = new B();
C object3 = new C();
myList.add(object1);
myList.add(object2);
myList.add(object3);
for (A object : myList) {
object.methodFromA();
if (object instanceof B) {
B castedObject = (B) object;
castedObject.methodFromB();
}
else if (object instanceof C) {
C castedObject = (C) object;
castedObject.methodFromC();
}
}
}
the resulting output:
From class A
From class A
From class B
From class A
From class C
As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.
add a comment |
up vote
0
down vote
First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.
But, anyway you can get item from arraylist and check it using instanceof
like:
if (item instanceof FirstSubClass){
FirstSubClass f = (FirstSubClass) item;
f.methodInFirst();
} else if (...)
.....
And how can I use the instance variables of that specific subclass?
– user10610048
Nov 11 at 22:01
Just cast it. I added logic in my code
– Vasif
Nov 11 at 22:05
If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
– user10610048
Nov 11 at 22:10
1
the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
– 4dc0
Nov 11 at 22:12
It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
– Vasif
Nov 11 at 22:14
|
show 4 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.
You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.
Here is an example:
public class A {
public void methodFromA() {
System.out.println("From class A");
}
}
public class B extends A {
public void methodFromB() {
System.out.println("From class B");
}
}
public class C extends A {
public void methodFromC() {
System.out.println("From class C");
}
}
and here are these classes in use, using an ArrayList with the superclass type:
public static void main(String args) {
List<A> myList = new ArrayList<>();
A object1 = new A();
B object2 = new B();
C object3 = new C();
myList.add(object1);
myList.add(object2);
myList.add(object3);
for (A object : myList) {
object.methodFromA();
if (object instanceof B) {
B castedObject = (B) object;
castedObject.methodFromB();
}
else if (object instanceof C) {
C castedObject = (C) object;
castedObject.methodFromC();
}
}
}
the resulting output:
From class A
From class A
From class B
From class A
From class C
As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.
add a comment |
up vote
0
down vote
accepted
Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.
You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.
Here is an example:
public class A {
public void methodFromA() {
System.out.println("From class A");
}
}
public class B extends A {
public void methodFromB() {
System.out.println("From class B");
}
}
public class C extends A {
public void methodFromC() {
System.out.println("From class C");
}
}
and here are these classes in use, using an ArrayList with the superclass type:
public static void main(String args) {
List<A> myList = new ArrayList<>();
A object1 = new A();
B object2 = new B();
C object3 = new C();
myList.add(object1);
myList.add(object2);
myList.add(object3);
for (A object : myList) {
object.methodFromA();
if (object instanceof B) {
B castedObject = (B) object;
castedObject.methodFromB();
}
else if (object instanceof C) {
C castedObject = (C) object;
castedObject.methodFromC();
}
}
}
the resulting output:
From class A
From class A
From class B
From class A
From class C
As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.
You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.
Here is an example:
public class A {
public void methodFromA() {
System.out.println("From class A");
}
}
public class B extends A {
public void methodFromB() {
System.out.println("From class B");
}
}
public class C extends A {
public void methodFromC() {
System.out.println("From class C");
}
}
and here are these classes in use, using an ArrayList with the superclass type:
public static void main(String args) {
List<A> myList = new ArrayList<>();
A object1 = new A();
B object2 = new B();
C object3 = new C();
myList.add(object1);
myList.add(object2);
myList.add(object3);
for (A object : myList) {
object.methodFromA();
if (object instanceof B) {
B castedObject = (B) object;
castedObject.methodFromB();
}
else if (object instanceof C) {
C castedObject = (C) object;
castedObject.methodFromC();
}
}
}
the resulting output:
From class A
From class A
From class B
From class A
From class C
As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.
Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.
You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.
Here is an example:
public class A {
public void methodFromA() {
System.out.println("From class A");
}
}
public class B extends A {
public void methodFromB() {
System.out.println("From class B");
}
}
public class C extends A {
public void methodFromC() {
System.out.println("From class C");
}
}
and here are these classes in use, using an ArrayList with the superclass type:
public static void main(String args) {
List<A> myList = new ArrayList<>();
A object1 = new A();
B object2 = new B();
C object3 = new C();
myList.add(object1);
myList.add(object2);
myList.add(object3);
for (A object : myList) {
object.methodFromA();
if (object instanceof B) {
B castedObject = (B) object;
castedObject.methodFromB();
}
else if (object instanceof C) {
C castedObject = (C) object;
castedObject.methodFromC();
}
}
}
the resulting output:
From class A
From class A
From class B
From class A
From class C
As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.
edited Nov 11 at 22:08
answered Nov 11 at 21:50
4dc0
42539
42539
add a comment |
add a comment |
up vote
0
down vote
First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.
But, anyway you can get item from arraylist and check it using instanceof
like:
if (item instanceof FirstSubClass){
FirstSubClass f = (FirstSubClass) item;
f.methodInFirst();
} else if (...)
.....
And how can I use the instance variables of that specific subclass?
– user10610048
Nov 11 at 22:01
Just cast it. I added logic in my code
– Vasif
Nov 11 at 22:05
If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
– user10610048
Nov 11 at 22:10
1
the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
– 4dc0
Nov 11 at 22:12
It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
– Vasif
Nov 11 at 22:14
|
show 4 more comments
up vote
0
down vote
First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.
But, anyway you can get item from arraylist and check it using instanceof
like:
if (item instanceof FirstSubClass){
FirstSubClass f = (FirstSubClass) item;
f.methodInFirst();
} else if (...)
.....
And how can I use the instance variables of that specific subclass?
– user10610048
Nov 11 at 22:01
Just cast it. I added logic in my code
– Vasif
Nov 11 at 22:05
If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
– user10610048
Nov 11 at 22:10
1
the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
– 4dc0
Nov 11 at 22:12
It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
– Vasif
Nov 11 at 22:14
|
show 4 more comments
up vote
0
down vote
up vote
0
down vote
First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.
But, anyway you can get item from arraylist and check it using instanceof
like:
if (item instanceof FirstSubClass){
FirstSubClass f = (FirstSubClass) item;
f.methodInFirst();
} else if (...)
.....
First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.
But, anyway you can get item from arraylist and check it using instanceof
like:
if (item instanceof FirstSubClass){
FirstSubClass f = (FirstSubClass) item;
f.methodInFirst();
} else if (...)
.....
edited Nov 11 at 22:04
answered Nov 11 at 21:57
Vasif
21815
21815
And how can I use the instance variables of that specific subclass?
– user10610048
Nov 11 at 22:01
Just cast it. I added logic in my code
– Vasif
Nov 11 at 22:05
If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
– user10610048
Nov 11 at 22:10
1
the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
– 4dc0
Nov 11 at 22:12
It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
– Vasif
Nov 11 at 22:14
|
show 4 more comments
And how can I use the instance variables of that specific subclass?
– user10610048
Nov 11 at 22:01
Just cast it. I added logic in my code
– Vasif
Nov 11 at 22:05
If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
– user10610048
Nov 11 at 22:10
1
the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
– 4dc0
Nov 11 at 22:12
It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
– Vasif
Nov 11 at 22:14
And how can I use the instance variables of that specific subclass?
– user10610048
Nov 11 at 22:01
And how can I use the instance variables of that specific subclass?
– user10610048
Nov 11 at 22:01
Just cast it. I added logic in my code
– Vasif
Nov 11 at 22:05
Just cast it. I added logic in my code
– Vasif
Nov 11 at 22:05
If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
– user10610048
Nov 11 at 22:10
If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
– user10610048
Nov 11 at 22:10
1
1
the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
– 4dc0
Nov 11 at 22:12
the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
– 4dc0
Nov 11 at 22:12
It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
– Vasif
Nov 11 at 22:14
It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
– Vasif
Nov 11 at 22:14
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253558%2fhow-to-use-the-methods-of-a-subclass-of-a-superclass-in-another-class-in-which-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown