using template parameters in java interface declaration
I come from C++ and I'm taking a Java class right now, studying design patterns more specifically. Last class, the professor gave us an example project to help us get started with the Prototype pattern and there was an interface declaration in the project which I didn't understand very well (and didn't ask the professor either :/)
package pattern.prototype.impl;
public interface IPrototype<T extends IPrototype> extends Cloneable {
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
could anyone give me some kind of explanation regarding the use of parameter T
in this context IPrototype<T extends IPrototype>
. What is its purpose there? Is it necessary or just one way of doing it?
Thank you
java templates interface prototype-pattern
add a comment |
I come from C++ and I'm taking a Java class right now, studying design patterns more specifically. Last class, the professor gave us an example project to help us get started with the Prototype pattern and there was an interface declaration in the project which I didn't understand very well (and didn't ask the professor either :/)
package pattern.prototype.impl;
public interface IPrototype<T extends IPrototype> extends Cloneable {
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
could anyone give me some kind of explanation regarding the use of parameter T
in this context IPrototype<T extends IPrototype>
. What is its purpose there? Is it necessary or just one way of doing it?
Thank you
java templates interface prototype-pattern
This called the "Curiously re-occurring template pattern".
– flakes
Nov 23 at 0:18
1
Java does not have C++ template parameters. That is a Java generic type. It is being namedT
, and declared thatT
must implementIPrototype
– Elliott Frisch
Nov 23 at 0:18
1
docs.oracle.com/javase/tutorial/java/generics/index.html
– Kartik
Nov 23 at 0:21
Was also going to say that Java generic types are not at all the same as C++ templates. Important to keep the two distinct in one's mind, in spite of the similar syntax.
– markspace
Nov 23 at 0:51
add a comment |
I come from C++ and I'm taking a Java class right now, studying design patterns more specifically. Last class, the professor gave us an example project to help us get started with the Prototype pattern and there was an interface declaration in the project which I didn't understand very well (and didn't ask the professor either :/)
package pattern.prototype.impl;
public interface IPrototype<T extends IPrototype> extends Cloneable {
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
could anyone give me some kind of explanation regarding the use of parameter T
in this context IPrototype<T extends IPrototype>
. What is its purpose there? Is it necessary or just one way of doing it?
Thank you
java templates interface prototype-pattern
I come from C++ and I'm taking a Java class right now, studying design patterns more specifically. Last class, the professor gave us an example project to help us get started with the Prototype pattern and there was an interface declaration in the project which I didn't understand very well (and didn't ask the professor either :/)
package pattern.prototype.impl;
public interface IPrototype<T extends IPrototype> extends Cloneable {
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
could anyone give me some kind of explanation regarding the use of parameter T
in this context IPrototype<T extends IPrototype>
. What is its purpose there? Is it necessary or just one way of doing it?
Thank you
java templates interface prototype-pattern
java templates interface prototype-pattern
asked Nov 23 at 0:11
Scaramouche
1,9891829
1,9891829
This called the "Curiously re-occurring template pattern".
– flakes
Nov 23 at 0:18
1
Java does not have C++ template parameters. That is a Java generic type. It is being namedT
, and declared thatT
must implementIPrototype
– Elliott Frisch
Nov 23 at 0:18
1
docs.oracle.com/javase/tutorial/java/generics/index.html
– Kartik
Nov 23 at 0:21
Was also going to say that Java generic types are not at all the same as C++ templates. Important to keep the two distinct in one's mind, in spite of the similar syntax.
– markspace
Nov 23 at 0:51
add a comment |
This called the "Curiously re-occurring template pattern".
– flakes
Nov 23 at 0:18
1
Java does not have C++ template parameters. That is a Java generic type. It is being namedT
, and declared thatT
must implementIPrototype
– Elliott Frisch
Nov 23 at 0:18
1
docs.oracle.com/javase/tutorial/java/generics/index.html
– Kartik
Nov 23 at 0:21
Was also going to say that Java generic types are not at all the same as C++ templates. Important to keep the two distinct in one's mind, in spite of the similar syntax.
– markspace
Nov 23 at 0:51
This called the "Curiously re-occurring template pattern".
– flakes
Nov 23 at 0:18
This called the "Curiously re-occurring template pattern".
– flakes
Nov 23 at 0:18
1
1
Java does not have C++ template parameters. That is a Java generic type. It is being named
T
, and declared that T
must implement IPrototype
– Elliott Frisch
Nov 23 at 0:18
Java does not have C++ template parameters. That is a Java generic type. It is being named
T
, and declared that T
must implement IPrototype
– Elliott Frisch
Nov 23 at 0:18
1
1
docs.oracle.com/javase/tutorial/java/generics/index.html
– Kartik
Nov 23 at 0:21
docs.oracle.com/javase/tutorial/java/generics/index.html
– Kartik
Nov 23 at 0:21
Was also going to say that Java generic types are not at all the same as C++ templates. Important to keep the two distinct in one's mind, in spite of the similar syntax.
– markspace
Nov 23 at 0:51
Was also going to say that Java generic types are not at all the same as C++ templates. Important to keep the two distinct in one's mind, in spite of the similar syntax.
– markspace
Nov 23 at 0:51
add a comment |
4 Answers
4
active
oldest
votes
This is called the "Curiously re-occurring template pattern". As the name suggests, it was discovered for code written in C++ which uses templates, however, the pattern works with Generics in Java as well.
Here I can implement the interface as follows:
public class ConcretePrototype implements IPrototype<ConcretePrototype > {
@Override
public ConcretePrototype clone() { ... }
@Override
public ConcretePrototype deepClone() { ... }
}
Notice the method signature of the overriden methods. The base interface IPrototype
does not know about ConcretePrototype
, however, by using CRTP we can enforce that ConcretePrototype
returns values of its own type.
from your answer, can you tell me if these two assumptions are correct, please? 1)<T extends IPrototype>
's only purpose is to allow us to declarepublic T clone();
usingT
. 2) if all the interface's methods were, for instance,void
, we would not need<T extends IPrototype>
.
– Scaramouche
Nov 23 at 1:48
@Scaramouche 1) For the code provided yes. It could also be used for parameters in the method signatures. 2) If you don't use the type then you don't need a type parameter. A slightly different way of writing the methods could be,IPrototype clone();
, but then you would have to potentially cast the result toConcretePrototype
, which may or may not cause you extra work. eg if you have a variableConcretePrototype p = new ConcretePrototype();
then do you want to writeConcretePrototype pClone = p.clone();
orConcretePrototype pClone = (ConcretePrototype) p.clone();
– flakes
Nov 23 at 1:54
great, you answered those two questions AND the third question I was about to ask, thank you, finally, do you really thinkIPrototype<T extends IPrototype>
should be changed to IPrototype<T extends IPrototype<T>> as markspace suggests?
– Scaramouche
Nov 23 at 1:56
@Scaramouche no problem! And yesIPrototype<T extends IPrototype<T>>
is a better way to write the class definition!
– flakes
Nov 23 at 1:59
@Scaramouche Without the type parameter you could writeclass ConcretePrototype implements IPrototype<IPrototype>
which is probably not what you want the authors of the child classes to do. This would lead to writing code that looks like the non-generic exampleConcretePrototype pClone = (ConcretePrototype) p.clone()
. It depends on how strict you want to be. And generally you want to complete the type parameters or at least define them as unbounded directly :interface IPrototype<T extends IPrototype<?>>
– flakes
Nov 23 at 2:06
add a comment |
It’s not a template parameter, but a Java generic type, and it stands for any class implementing the given interface. In the context of prototype pattern it’s not necessary, just one possible implementation.
add a comment |
T is type parameter also a generic type.
T extends IPrototype : To declare a bounded type parameter,T can be any type that is subclass of IPrototype
public T clone(); return type would be generic.
public T deepClone(); return type would be generic(means can be any type)
add a comment |
BTW, I'm pretty sure the syntax you are using is wrong, so it might be best to inform your instructor anyway.
The problem is that IPrototype
is used as a raw type here. The second time it's used on the line, it's just IPrototype
with no type variable. That's a no-no in Java.
As for what is going on, all it means is that the type parameter for IPrototype
must be a type of IPrototype
--that is, some subclass of IProtoType
. Take a look at Java's Enum
type, which uses exactly the same pattern: https://docs.oracle.com/javase/10/docs/api/java/lang/Enum.html
public interface IPrototype<T extends IPrototype<T>> extends Cloneable {
// ^^^ add this
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53439303%2fusing-template-parameters-in-java-interface-declaration%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is called the "Curiously re-occurring template pattern". As the name suggests, it was discovered for code written in C++ which uses templates, however, the pattern works with Generics in Java as well.
Here I can implement the interface as follows:
public class ConcretePrototype implements IPrototype<ConcretePrototype > {
@Override
public ConcretePrototype clone() { ... }
@Override
public ConcretePrototype deepClone() { ... }
}
Notice the method signature of the overriden methods. The base interface IPrototype
does not know about ConcretePrototype
, however, by using CRTP we can enforce that ConcretePrototype
returns values of its own type.
from your answer, can you tell me if these two assumptions are correct, please? 1)<T extends IPrototype>
's only purpose is to allow us to declarepublic T clone();
usingT
. 2) if all the interface's methods were, for instance,void
, we would not need<T extends IPrototype>
.
– Scaramouche
Nov 23 at 1:48
@Scaramouche 1) For the code provided yes. It could also be used for parameters in the method signatures. 2) If you don't use the type then you don't need a type parameter. A slightly different way of writing the methods could be,IPrototype clone();
, but then you would have to potentially cast the result toConcretePrototype
, which may or may not cause you extra work. eg if you have a variableConcretePrototype p = new ConcretePrototype();
then do you want to writeConcretePrototype pClone = p.clone();
orConcretePrototype pClone = (ConcretePrototype) p.clone();
– flakes
Nov 23 at 1:54
great, you answered those two questions AND the third question I was about to ask, thank you, finally, do you really thinkIPrototype<T extends IPrototype>
should be changed to IPrototype<T extends IPrototype<T>> as markspace suggests?
– Scaramouche
Nov 23 at 1:56
@Scaramouche no problem! And yesIPrototype<T extends IPrototype<T>>
is a better way to write the class definition!
– flakes
Nov 23 at 1:59
@Scaramouche Without the type parameter you could writeclass ConcretePrototype implements IPrototype<IPrototype>
which is probably not what you want the authors of the child classes to do. This would lead to writing code that looks like the non-generic exampleConcretePrototype pClone = (ConcretePrototype) p.clone()
. It depends on how strict you want to be. And generally you want to complete the type parameters or at least define them as unbounded directly :interface IPrototype<T extends IPrototype<?>>
– flakes
Nov 23 at 2:06
add a comment |
This is called the "Curiously re-occurring template pattern". As the name suggests, it was discovered for code written in C++ which uses templates, however, the pattern works with Generics in Java as well.
Here I can implement the interface as follows:
public class ConcretePrototype implements IPrototype<ConcretePrototype > {
@Override
public ConcretePrototype clone() { ... }
@Override
public ConcretePrototype deepClone() { ... }
}
Notice the method signature of the overriden methods. The base interface IPrototype
does not know about ConcretePrototype
, however, by using CRTP we can enforce that ConcretePrototype
returns values of its own type.
from your answer, can you tell me if these two assumptions are correct, please? 1)<T extends IPrototype>
's only purpose is to allow us to declarepublic T clone();
usingT
. 2) if all the interface's methods were, for instance,void
, we would not need<T extends IPrototype>
.
– Scaramouche
Nov 23 at 1:48
@Scaramouche 1) For the code provided yes. It could also be used for parameters in the method signatures. 2) If you don't use the type then you don't need a type parameter. A slightly different way of writing the methods could be,IPrototype clone();
, but then you would have to potentially cast the result toConcretePrototype
, which may or may not cause you extra work. eg if you have a variableConcretePrototype p = new ConcretePrototype();
then do you want to writeConcretePrototype pClone = p.clone();
orConcretePrototype pClone = (ConcretePrototype) p.clone();
– flakes
Nov 23 at 1:54
great, you answered those two questions AND the third question I was about to ask, thank you, finally, do you really thinkIPrototype<T extends IPrototype>
should be changed to IPrototype<T extends IPrototype<T>> as markspace suggests?
– Scaramouche
Nov 23 at 1:56
@Scaramouche no problem! And yesIPrototype<T extends IPrototype<T>>
is a better way to write the class definition!
– flakes
Nov 23 at 1:59
@Scaramouche Without the type parameter you could writeclass ConcretePrototype implements IPrototype<IPrototype>
which is probably not what you want the authors of the child classes to do. This would lead to writing code that looks like the non-generic exampleConcretePrototype pClone = (ConcretePrototype) p.clone()
. It depends on how strict you want to be. And generally you want to complete the type parameters or at least define them as unbounded directly :interface IPrototype<T extends IPrototype<?>>
– flakes
Nov 23 at 2:06
add a comment |
This is called the "Curiously re-occurring template pattern". As the name suggests, it was discovered for code written in C++ which uses templates, however, the pattern works with Generics in Java as well.
Here I can implement the interface as follows:
public class ConcretePrototype implements IPrototype<ConcretePrototype > {
@Override
public ConcretePrototype clone() { ... }
@Override
public ConcretePrototype deepClone() { ... }
}
Notice the method signature of the overriden methods. The base interface IPrototype
does not know about ConcretePrototype
, however, by using CRTP we can enforce that ConcretePrototype
returns values of its own type.
This is called the "Curiously re-occurring template pattern". As the name suggests, it was discovered for code written in C++ which uses templates, however, the pattern works with Generics in Java as well.
Here I can implement the interface as follows:
public class ConcretePrototype implements IPrototype<ConcretePrototype > {
@Override
public ConcretePrototype clone() { ... }
@Override
public ConcretePrototype deepClone() { ... }
}
Notice the method signature of the overriden methods. The base interface IPrototype
does not know about ConcretePrototype
, however, by using CRTP we can enforce that ConcretePrototype
returns values of its own type.
answered Nov 23 at 0:25
flakes
6,51611850
6,51611850
from your answer, can you tell me if these two assumptions are correct, please? 1)<T extends IPrototype>
's only purpose is to allow us to declarepublic T clone();
usingT
. 2) if all the interface's methods were, for instance,void
, we would not need<T extends IPrototype>
.
– Scaramouche
Nov 23 at 1:48
@Scaramouche 1) For the code provided yes. It could also be used for parameters in the method signatures. 2) If you don't use the type then you don't need a type parameter. A slightly different way of writing the methods could be,IPrototype clone();
, but then you would have to potentially cast the result toConcretePrototype
, which may or may not cause you extra work. eg if you have a variableConcretePrototype p = new ConcretePrototype();
then do you want to writeConcretePrototype pClone = p.clone();
orConcretePrototype pClone = (ConcretePrototype) p.clone();
– flakes
Nov 23 at 1:54
great, you answered those two questions AND the third question I was about to ask, thank you, finally, do you really thinkIPrototype<T extends IPrototype>
should be changed to IPrototype<T extends IPrototype<T>> as markspace suggests?
– Scaramouche
Nov 23 at 1:56
@Scaramouche no problem! And yesIPrototype<T extends IPrototype<T>>
is a better way to write the class definition!
– flakes
Nov 23 at 1:59
@Scaramouche Without the type parameter you could writeclass ConcretePrototype implements IPrototype<IPrototype>
which is probably not what you want the authors of the child classes to do. This would lead to writing code that looks like the non-generic exampleConcretePrototype pClone = (ConcretePrototype) p.clone()
. It depends on how strict you want to be. And generally you want to complete the type parameters or at least define them as unbounded directly :interface IPrototype<T extends IPrototype<?>>
– flakes
Nov 23 at 2:06
add a comment |
from your answer, can you tell me if these two assumptions are correct, please? 1)<T extends IPrototype>
's only purpose is to allow us to declarepublic T clone();
usingT
. 2) if all the interface's methods were, for instance,void
, we would not need<T extends IPrototype>
.
– Scaramouche
Nov 23 at 1:48
@Scaramouche 1) For the code provided yes. It could also be used for parameters in the method signatures. 2) If you don't use the type then you don't need a type parameter. A slightly different way of writing the methods could be,IPrototype clone();
, but then you would have to potentially cast the result toConcretePrototype
, which may or may not cause you extra work. eg if you have a variableConcretePrototype p = new ConcretePrototype();
then do you want to writeConcretePrototype pClone = p.clone();
orConcretePrototype pClone = (ConcretePrototype) p.clone();
– flakes
Nov 23 at 1:54
great, you answered those two questions AND the third question I was about to ask, thank you, finally, do you really thinkIPrototype<T extends IPrototype>
should be changed to IPrototype<T extends IPrototype<T>> as markspace suggests?
– Scaramouche
Nov 23 at 1:56
@Scaramouche no problem! And yesIPrototype<T extends IPrototype<T>>
is a better way to write the class definition!
– flakes
Nov 23 at 1:59
@Scaramouche Without the type parameter you could writeclass ConcretePrototype implements IPrototype<IPrototype>
which is probably not what you want the authors of the child classes to do. This would lead to writing code that looks like the non-generic exampleConcretePrototype pClone = (ConcretePrototype) p.clone()
. It depends on how strict you want to be. And generally you want to complete the type parameters or at least define them as unbounded directly :interface IPrototype<T extends IPrototype<?>>
– flakes
Nov 23 at 2:06
from your answer, can you tell me if these two assumptions are correct, please? 1)
<T extends IPrototype>
's only purpose is to allow us to declare public T clone();
using T
. 2) if all the interface's methods were, for instance, void
, we would not need <T extends IPrototype>
.– Scaramouche
Nov 23 at 1:48
from your answer, can you tell me if these two assumptions are correct, please? 1)
<T extends IPrototype>
's only purpose is to allow us to declare public T clone();
using T
. 2) if all the interface's methods were, for instance, void
, we would not need <T extends IPrototype>
.– Scaramouche
Nov 23 at 1:48
@Scaramouche 1) For the code provided yes. It could also be used for parameters in the method signatures. 2) If you don't use the type then you don't need a type parameter. A slightly different way of writing the methods could be,
IPrototype clone();
, but then you would have to potentially cast the result to ConcretePrototype
, which may or may not cause you extra work. eg if you have a variable ConcretePrototype p = new ConcretePrototype();
then do you want to write ConcretePrototype pClone = p.clone();
or ConcretePrototype pClone = (ConcretePrototype) p.clone();
– flakes
Nov 23 at 1:54
@Scaramouche 1) For the code provided yes. It could also be used for parameters in the method signatures. 2) If you don't use the type then you don't need a type parameter. A slightly different way of writing the methods could be,
IPrototype clone();
, but then you would have to potentially cast the result to ConcretePrototype
, which may or may not cause you extra work. eg if you have a variable ConcretePrototype p = new ConcretePrototype();
then do you want to write ConcretePrototype pClone = p.clone();
or ConcretePrototype pClone = (ConcretePrototype) p.clone();
– flakes
Nov 23 at 1:54
great, you answered those two questions AND the third question I was about to ask, thank you, finally, do you really think
IPrototype<T extends IPrototype>
should be changed to IPrototype<T extends IPrototype<T>> as markspace suggests?– Scaramouche
Nov 23 at 1:56
great, you answered those two questions AND the third question I was about to ask, thank you, finally, do you really think
IPrototype<T extends IPrototype>
should be changed to IPrototype<T extends IPrototype<T>> as markspace suggests?– Scaramouche
Nov 23 at 1:56
@Scaramouche no problem! And yes
IPrototype<T extends IPrototype<T>>
is a better way to write the class definition!– flakes
Nov 23 at 1:59
@Scaramouche no problem! And yes
IPrototype<T extends IPrototype<T>>
is a better way to write the class definition!– flakes
Nov 23 at 1:59
@Scaramouche Without the type parameter you could write
class ConcretePrototype implements IPrototype<IPrototype>
which is probably not what you want the authors of the child classes to do. This would lead to writing code that looks like the non-generic example ConcretePrototype pClone = (ConcretePrototype) p.clone()
. It depends on how strict you want to be. And generally you want to complete the type parameters or at least define them as unbounded directly : interface IPrototype<T extends IPrototype<?>>
– flakes
Nov 23 at 2:06
@Scaramouche Without the type parameter you could write
class ConcretePrototype implements IPrototype<IPrototype>
which is probably not what you want the authors of the child classes to do. This would lead to writing code that looks like the non-generic example ConcretePrototype pClone = (ConcretePrototype) p.clone()
. It depends on how strict you want to be. And generally you want to complete the type parameters or at least define them as unbounded directly : interface IPrototype<T extends IPrototype<?>>
– flakes
Nov 23 at 2:06
add a comment |
It’s not a template parameter, but a Java generic type, and it stands for any class implementing the given interface. In the context of prototype pattern it’s not necessary, just one possible implementation.
add a comment |
It’s not a template parameter, but a Java generic type, and it stands for any class implementing the given interface. In the context of prototype pattern it’s not necessary, just one possible implementation.
add a comment |
It’s not a template parameter, but a Java generic type, and it stands for any class implementing the given interface. In the context of prototype pattern it’s not necessary, just one possible implementation.
It’s not a template parameter, but a Java generic type, and it stands for any class implementing the given interface. In the context of prototype pattern it’s not necessary, just one possible implementation.
answered Nov 23 at 0:26
Nenad
1408
1408
add a comment |
add a comment |
T is type parameter also a generic type.
T extends IPrototype : To declare a bounded type parameter,T can be any type that is subclass of IPrototype
public T clone(); return type would be generic.
public T deepClone(); return type would be generic(means can be any type)
add a comment |
T is type parameter also a generic type.
T extends IPrototype : To declare a bounded type parameter,T can be any type that is subclass of IPrototype
public T clone(); return type would be generic.
public T deepClone(); return type would be generic(means can be any type)
add a comment |
T is type parameter also a generic type.
T extends IPrototype : To declare a bounded type parameter,T can be any type that is subclass of IPrototype
public T clone(); return type would be generic.
public T deepClone(); return type would be generic(means can be any type)
T is type parameter also a generic type.
T extends IPrototype : To declare a bounded type parameter,T can be any type that is subclass of IPrototype
public T clone(); return type would be generic.
public T deepClone(); return type would be generic(means can be any type)
answered Nov 23 at 0:28
GauravRai1512
59811
59811
add a comment |
add a comment |
BTW, I'm pretty sure the syntax you are using is wrong, so it might be best to inform your instructor anyway.
The problem is that IPrototype
is used as a raw type here. The second time it's used on the line, it's just IPrototype
with no type variable. That's a no-no in Java.
As for what is going on, all it means is that the type parameter for IPrototype
must be a type of IPrototype
--that is, some subclass of IProtoType
. Take a look at Java's Enum
type, which uses exactly the same pattern: https://docs.oracle.com/javase/10/docs/api/java/lang/Enum.html
public interface IPrototype<T extends IPrototype<T>> extends Cloneable {
// ^^^ add this
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
add a comment |
BTW, I'm pretty sure the syntax you are using is wrong, so it might be best to inform your instructor anyway.
The problem is that IPrototype
is used as a raw type here. The second time it's used on the line, it's just IPrototype
with no type variable. That's a no-no in Java.
As for what is going on, all it means is that the type parameter for IPrototype
must be a type of IPrototype
--that is, some subclass of IProtoType
. Take a look at Java's Enum
type, which uses exactly the same pattern: https://docs.oracle.com/javase/10/docs/api/java/lang/Enum.html
public interface IPrototype<T extends IPrototype<T>> extends Cloneable {
// ^^^ add this
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
add a comment |
BTW, I'm pretty sure the syntax you are using is wrong, so it might be best to inform your instructor anyway.
The problem is that IPrototype
is used as a raw type here. The second time it's used on the line, it's just IPrototype
with no type variable. That's a no-no in Java.
As for what is going on, all it means is that the type parameter for IPrototype
must be a type of IPrototype
--that is, some subclass of IProtoType
. Take a look at Java's Enum
type, which uses exactly the same pattern: https://docs.oracle.com/javase/10/docs/api/java/lang/Enum.html
public interface IPrototype<T extends IPrototype<T>> extends Cloneable {
// ^^^ add this
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
BTW, I'm pretty sure the syntax you are using is wrong, so it might be best to inform your instructor anyway.
The problem is that IPrototype
is used as a raw type here. The second time it's used on the line, it's just IPrototype
with no type variable. That's a no-no in Java.
As for what is going on, all it means is that the type parameter for IPrototype
must be a type of IPrototype
--that is, some subclass of IProtoType
. Take a look at Java's Enum
type, which uses exactly the same pattern: https://docs.oracle.com/javase/10/docs/api/java/lang/Enum.html
public interface IPrototype<T extends IPrototype<T>> extends Cloneable {
// ^^^ add this
//clone: Permite realizar una clonacion superficial del prototipo.
public T clone();
//deepClone: Permite realizar una clonación profunda del prototipo.
public T deepClone();
}
edited Nov 23 at 1:02
answered Nov 23 at 0:56
markspace
6,82321526
6,82321526
add a comment |
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%2f53439303%2fusing-template-parameters-in-java-interface-declaration%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
This called the "Curiously re-occurring template pattern".
– flakes
Nov 23 at 0:18
1
Java does not have C++ template parameters. That is a Java generic type. It is being named
T
, and declared thatT
must implementIPrototype
– Elliott Frisch
Nov 23 at 0:18
1
docs.oracle.com/javase/tutorial/java/generics/index.html
– Kartik
Nov 23 at 0:21
Was also going to say that Java generic types are not at all the same as C++ templates. Important to keep the two distinct in one's mind, in spite of the similar syntax.
– markspace
Nov 23 at 0:51