Force subclass to override method with itself as parameter
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
I have an abstract Event class which has an abstract method:
abstract boolean intersect(Event object);
This method should check if two instances of an Event subclass intersect based on the instance variables of the particular subclass. I want to force any subclass of Event to override the method on its instance variables. What is the best way to design this? This is my current implementation, which is wrong since I am changing the parameter type. I have also tried using interfaces, but have run into similar problems with type parameters.
@Override
public boolean intersect(SubClassEvent e2)
boolean intersects = false;
if(this.weekDay == e2.weekDay)
if (this.getStartTime() < e2.getStartTime() && this.getEndTime() > e2.getStartTime())
intersects = true;
else if(this.getStartTime() >= e2.getStartTime() && this.getStartTime() < e2.getEndTime())
intersects = true;
return intersects;
java generics inheritance casting type-parameter
add a comment |Â
up vote
9
down vote
favorite
I have an abstract Event class which has an abstract method:
abstract boolean intersect(Event object);
This method should check if two instances of an Event subclass intersect based on the instance variables of the particular subclass. I want to force any subclass of Event to override the method on its instance variables. What is the best way to design this? This is my current implementation, which is wrong since I am changing the parameter type. I have also tried using interfaces, but have run into similar problems with type parameters.
@Override
public boolean intersect(SubClassEvent e2)
boolean intersects = false;
if(this.weekDay == e2.weekDay)
if (this.getStartTime() < e2.getStartTime() && this.getEndTime() > e2.getStartTime())
intersects = true;
else if(this.getStartTime() >= e2.getStartTime() && this.getStartTime() < e2.getEndTime())
intersects = true;
return intersects;
java generics inheritance casting type-parameter
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I have an abstract Event class which has an abstract method:
abstract boolean intersect(Event object);
This method should check if two instances of an Event subclass intersect based on the instance variables of the particular subclass. I want to force any subclass of Event to override the method on its instance variables. What is the best way to design this? This is my current implementation, which is wrong since I am changing the parameter type. I have also tried using interfaces, but have run into similar problems with type parameters.
@Override
public boolean intersect(SubClassEvent e2)
boolean intersects = false;
if(this.weekDay == e2.weekDay)
if (this.getStartTime() < e2.getStartTime() && this.getEndTime() > e2.getStartTime())
intersects = true;
else if(this.getStartTime() >= e2.getStartTime() && this.getStartTime() < e2.getEndTime())
intersects = true;
return intersects;
java generics inheritance casting type-parameter
I have an abstract Event class which has an abstract method:
abstract boolean intersect(Event object);
This method should check if two instances of an Event subclass intersect based on the instance variables of the particular subclass. I want to force any subclass of Event to override the method on its instance variables. What is the best way to design this? This is my current implementation, which is wrong since I am changing the parameter type. I have also tried using interfaces, but have run into similar problems with type parameters.
@Override
public boolean intersect(SubClassEvent e2)
boolean intersects = false;
if(this.weekDay == e2.weekDay)
if (this.getStartTime() < e2.getStartTime() && this.getEndTime() > e2.getStartTime())
intersects = true;
else if(this.getStartTime() >= e2.getStartTime() && this.getStartTime() < e2.getEndTime())
intersects = true;
return intersects;
java generics inheritance casting type-parameter
java generics inheritance casting type-parameter
edited 15 mins ago
Mark Rotteveel
57.7k1473114
57.7k1473114
asked 4 hours ago
Nicolae Stroncea
10819
10819
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
10
down vote
accepted
If you make the abstract class generic, you can achieve that:
abstract class Event<T extends Event<T>>
abstract boolean intersect(T object);
Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.
class SubClassEvent extends Event<SubClassEvent>
@Override
boolean intersect(SubClassEvent object)return true;
Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
– Nicolae Stroncea
4 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
If you make the abstract class generic, you can achieve that:
abstract class Event<T extends Event<T>>
abstract boolean intersect(T object);
Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.
class SubClassEvent extends Event<SubClassEvent>
@Override
boolean intersect(SubClassEvent object)return true;
Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
– Nicolae Stroncea
4 hours ago
add a comment |Â
up vote
10
down vote
accepted
If you make the abstract class generic, you can achieve that:
abstract class Event<T extends Event<T>>
abstract boolean intersect(T object);
Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.
class SubClassEvent extends Event<SubClassEvent>
@Override
boolean intersect(SubClassEvent object)return true;
Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
– Nicolae Stroncea
4 hours ago
add a comment |Â
up vote
10
down vote
accepted
up vote
10
down vote
accepted
If you make the abstract class generic, you can achieve that:
abstract class Event<T extends Event<T>>
abstract boolean intersect(T object);
Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.
class SubClassEvent extends Event<SubClassEvent>
@Override
boolean intersect(SubClassEvent object)return true;
If you make the abstract class generic, you can achieve that:
abstract class Event<T extends Event<T>>
abstract boolean intersect(T object);
Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.
class SubClassEvent extends Event<SubClassEvent>
@Override
boolean intersect(SubClassEvent object)return true;
edited 26 mins ago
answered 4 hours ago
ernest_k
17.1k41535
17.1k41535
Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
– Nicolae Stroncea
4 hours ago
add a comment |Â
Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
– Nicolae Stroncea
4 hours ago
Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
– Nicolae Stroncea
4 hours ago
Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
– Nicolae Stroncea
4 hours ago
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148650%2fforce-subclass-to-override-method-with-itself-as-parameter%23new-answer', 'question_page');
);
Post as a guest
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
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
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