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?










share|improve this question
























  • 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















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?










share|improve this question
























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine