can we make use of polymorphism by inheriting an empty class?
up vote
-2
down vote
favorite
I want to use Generics concept and create an ArrayList that needs to take different types of objects so I want to create an empty class called ObjectType
and inherit it via 'Person and Animal
' classes and want to create ArrayList as below
List<? extends ObjectType> list=new ArrayList<Persons>();
but it is not working what is the solution?
java
add a comment |
up vote
-2
down vote
favorite
I want to use Generics concept and create an ArrayList that needs to take different types of objects so I want to create an empty class called ObjectType
and inherit it via 'Person and Animal
' classes and want to create ArrayList as below
List<? extends ObjectType> list=new ArrayList<Persons>();
but it is not working what is the solution?
java
Can you mention your class implementation?
– GauravRai1512
Nov 11 at 12:33
public class ObjectType(){ public class Person extends ObjectType(){ public name,location,id,salary; //followed by a constructor for initialisation } then want to add perosn objects to arraylist as below List<? extends ObjectType> list=new ArrayList<Person>();
– Nagendhra Kumar Sunkara
Nov 11 at 12:35
If you are creating empty class then i would suggest to use interface rather then class and implement your animal and person class.
– GauravRai1512
Nov 11 at 12:37
There is one correction public class ObjectType{} rather then public class ObjectType(){}
– GauravRai1512
Nov 11 at 12:43
1
Don't put more information into comments, always update your question instead. Or can you really read that code only comments you made?
– GhostCat
Nov 11 at 12:45
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I want to use Generics concept and create an ArrayList that needs to take different types of objects so I want to create an empty class called ObjectType
and inherit it via 'Person and Animal
' classes and want to create ArrayList as below
List<? extends ObjectType> list=new ArrayList<Persons>();
but it is not working what is the solution?
java
I want to use Generics concept and create an ArrayList that needs to take different types of objects so I want to create an empty class called ObjectType
and inherit it via 'Person and Animal
' classes and want to create ArrayList as below
List<? extends ObjectType> list=new ArrayList<Persons>();
but it is not working what is the solution?
java
java
edited Nov 11 at 13:15
Gihan Saranga Siriwardhana
613217
613217
asked Nov 11 at 12:31
Nagendhra Kumar Sunkara
1
1
Can you mention your class implementation?
– GauravRai1512
Nov 11 at 12:33
public class ObjectType(){ public class Person extends ObjectType(){ public name,location,id,salary; //followed by a constructor for initialisation } then want to add perosn objects to arraylist as below List<? extends ObjectType> list=new ArrayList<Person>();
– Nagendhra Kumar Sunkara
Nov 11 at 12:35
If you are creating empty class then i would suggest to use interface rather then class and implement your animal and person class.
– GauravRai1512
Nov 11 at 12:37
There is one correction public class ObjectType{} rather then public class ObjectType(){}
– GauravRai1512
Nov 11 at 12:43
1
Don't put more information into comments, always update your question instead. Or can you really read that code only comments you made?
– GhostCat
Nov 11 at 12:45
add a comment |
Can you mention your class implementation?
– GauravRai1512
Nov 11 at 12:33
public class ObjectType(){ public class Person extends ObjectType(){ public name,location,id,salary; //followed by a constructor for initialisation } then want to add perosn objects to arraylist as below List<? extends ObjectType> list=new ArrayList<Person>();
– Nagendhra Kumar Sunkara
Nov 11 at 12:35
If you are creating empty class then i would suggest to use interface rather then class and implement your animal and person class.
– GauravRai1512
Nov 11 at 12:37
There is one correction public class ObjectType{} rather then public class ObjectType(){}
– GauravRai1512
Nov 11 at 12:43
1
Don't put more information into comments, always update your question instead. Or can you really read that code only comments you made?
– GhostCat
Nov 11 at 12:45
Can you mention your class implementation?
– GauravRai1512
Nov 11 at 12:33
Can you mention your class implementation?
– GauravRai1512
Nov 11 at 12:33
public class ObjectType(){ public class Person extends ObjectType(){ public name,location,id,salary; //followed by a constructor for initialisation } then want to add perosn objects to arraylist as below List<? extends ObjectType> list=new ArrayList<Person>();
– Nagendhra Kumar Sunkara
Nov 11 at 12:35
public class ObjectType(){ public class Person extends ObjectType(){ public name,location,id,salary; //followed by a constructor for initialisation } then want to add perosn objects to arraylist as below List<? extends ObjectType> list=new ArrayList<Person>();
– Nagendhra Kumar Sunkara
Nov 11 at 12:35
If you are creating empty class then i would suggest to use interface rather then class and implement your animal and person class.
– GauravRai1512
Nov 11 at 12:37
If you are creating empty class then i would suggest to use interface rather then class and implement your animal and person class.
– GauravRai1512
Nov 11 at 12:37
There is one correction public class ObjectType{} rather then public class ObjectType(){}
– GauravRai1512
Nov 11 at 12:43
There is one correction public class ObjectType{} rather then public class ObjectType(){}
– GauravRai1512
Nov 11 at 12:43
1
1
Don't put more information into comments, always update your question instead. Or can you really read that code only comments you made?
– GhostCat
Nov 11 at 12:45
Don't put more information into comments, always update your question instead. Or can you really read that code only comments you made?
– GhostCat
Nov 11 at 12:45
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
In general, I would use an interface
rather than a class wherever possible as it is simpler and more flexible.
What you can write is
interface ObjectType { }
class Animal implements ObjectType { }
class Person implements ObjectType { }
List<ObjectType> objects = new ArrayList<>();
This might not do what you expect, however how you solve this depends on what you need the List to do.
Yeah, but I need to use Generics(? extends ObjectType) in collection
– Nagendhra Kumar Sunkara
Nov 11 at 12:40
@NagendhraKumarSunkara If you do, you won't be able to add anything to such a collection. Can you provide a more detailed example?
– Peter Lawrey
Nov 11 at 12:42
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
In general, I would use an interface
rather than a class wherever possible as it is simpler and more flexible.
What you can write is
interface ObjectType { }
class Animal implements ObjectType { }
class Person implements ObjectType { }
List<ObjectType> objects = new ArrayList<>();
This might not do what you expect, however how you solve this depends on what you need the List to do.
Yeah, but I need to use Generics(? extends ObjectType) in collection
– Nagendhra Kumar Sunkara
Nov 11 at 12:40
@NagendhraKumarSunkara If you do, you won't be able to add anything to such a collection. Can you provide a more detailed example?
– Peter Lawrey
Nov 11 at 12:42
add a comment |
up vote
0
down vote
In general, I would use an interface
rather than a class wherever possible as it is simpler and more flexible.
What you can write is
interface ObjectType { }
class Animal implements ObjectType { }
class Person implements ObjectType { }
List<ObjectType> objects = new ArrayList<>();
This might not do what you expect, however how you solve this depends on what you need the List to do.
Yeah, but I need to use Generics(? extends ObjectType) in collection
– Nagendhra Kumar Sunkara
Nov 11 at 12:40
@NagendhraKumarSunkara If you do, you won't be able to add anything to such a collection. Can you provide a more detailed example?
– Peter Lawrey
Nov 11 at 12:42
add a comment |
up vote
0
down vote
up vote
0
down vote
In general, I would use an interface
rather than a class wherever possible as it is simpler and more flexible.
What you can write is
interface ObjectType { }
class Animal implements ObjectType { }
class Person implements ObjectType { }
List<ObjectType> objects = new ArrayList<>();
This might not do what you expect, however how you solve this depends on what you need the List to do.
In general, I would use an interface
rather than a class wherever possible as it is simpler and more flexible.
What you can write is
interface ObjectType { }
class Animal implements ObjectType { }
class Person implements ObjectType { }
List<ObjectType> objects = new ArrayList<>();
This might not do what you expect, however how you solve this depends on what you need the List to do.
answered Nov 11 at 12:35
Peter Lawrey
438k55556952
438k55556952
Yeah, but I need to use Generics(? extends ObjectType) in collection
– Nagendhra Kumar Sunkara
Nov 11 at 12:40
@NagendhraKumarSunkara If you do, you won't be able to add anything to such a collection. Can you provide a more detailed example?
– Peter Lawrey
Nov 11 at 12:42
add a comment |
Yeah, but I need to use Generics(? extends ObjectType) in collection
– Nagendhra Kumar Sunkara
Nov 11 at 12:40
@NagendhraKumarSunkara If you do, you won't be able to add anything to such a collection. Can you provide a more detailed example?
– Peter Lawrey
Nov 11 at 12:42
Yeah, but I need to use Generics(? extends ObjectType) in collection
– Nagendhra Kumar Sunkara
Nov 11 at 12:40
Yeah, but I need to use Generics(? extends ObjectType) in collection
– Nagendhra Kumar Sunkara
Nov 11 at 12:40
@NagendhraKumarSunkara If you do, you won't be able to add anything to such a collection. Can you provide a more detailed example?
– Peter Lawrey
Nov 11 at 12:42
@NagendhraKumarSunkara If you do, you won't be able to add anything to such a collection. Can you provide a more detailed example?
– Peter Lawrey
Nov 11 at 12:42
add a comment |
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%2f53248794%2fcan-we-make-use-of-polymorphism-by-inheriting-an-empty-class%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
Can you mention your class implementation?
– GauravRai1512
Nov 11 at 12:33
public class ObjectType(){ public class Person extends ObjectType(){ public name,location,id,salary; //followed by a constructor for initialisation } then want to add perosn objects to arraylist as below List<? extends ObjectType> list=new ArrayList<Person>();
– Nagendhra Kumar Sunkara
Nov 11 at 12:35
If you are creating empty class then i would suggest to use interface rather then class and implement your animal and person class.
– GauravRai1512
Nov 11 at 12:37
There is one correction public class ObjectType{} rather then public class ObjectType(){}
– GauravRai1512
Nov 11 at 12:43
1
Don't put more information into comments, always update your question instead. Or can you really read that code only comments you made?
– GhostCat
Nov 11 at 12:45