Java Inheritance: empty method in parent class The Next CEO of Stack OverflowIs Java “pass-by-reference” or “pass-by-value”?Prefer composition over inheritance?Java inner class and static nested classHow do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Understanding Python super() with __init__() methodsPython class inherits objectHow do I convert a String to an int in Java?Creating a memory leak with Java
Why was Sir Cadogan fired?
What happens if you break a law in another country outside of that country?
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
How should I connect my cat5 cable to connectors having an orange-green line?
Calculate the Mean mean of two numbers
How seriously should I take size and weight limits of hand luggage?
What difference does it make matching a word with/without a trailing whitespace?
Another proof that dividing by 0 does not exist -- is it right?
Ising model simulation
Oldie but Goldie
Can this transistor (2N2222) take 6 V on emitter-base? Am I reading the datasheet incorrectly?
Could a dragon use its wings to swim?
Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico
Raspberry pi 3 B with Ubuntu 18.04 server arm64: what pi version
pgfplots: How to draw a tangent graph below two others?
Prodigo = pro + ago?
Could you use a laser beam as a modulated carrier wave for radio signal?
Why can't we say "I have been having a dog"?
Avoiding the "not like other girls" trope?
Can Sri Krishna be called 'a person'?
Physiological effects of huge anime eyes
Is it correct to say moon starry nights?
Cannot restore registry to default in Windows 10?
Are British MPs missing the point, with these 'Indicative Votes'?
Java Inheritance: empty method in parent class
The Next CEO of Stack OverflowIs Java “pass-by-reference” or “pass-by-value”?Prefer composition over inheritance?Java inner class and static nested classHow do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Understanding Python super() with __init__() methodsPython class inherits objectHow do I convert a String to an int in Java?Creating a memory leak with Java
I was wondering if it is frowned upon to have an empty function in a parent class for child classes to override if they needed it? I personally think the use case below justifies it -
class Foo
void performAdditionalChecksIfNeeded(String decrypted)
void validate(String encrypted)
final String decrypted = decryptIfProperlyEncrypted(encrypted);
performAdditionalChecksIfNeeded(decrypted);
class Bar extends Foo
@override
void performAdditionalChecksIfNeeded(String decrypted)
// Additional validation logic
I'm aware that Bar
class could've simply overridden validate()
and called super.validate()
to eliminate the need of the empty function in Foo
. The reason why I had the code arranged this way is that decryptIfProperlyEncrypted
uses a library that I wanted to keep only in the Foo
class due to separation of concern, performance concerns (decryption), and code duplication avoidance. In other words, if performAdditionalChecksIfNeeded
didn't exist in Foo
I'd have to perform the decryption again in Bar
.
java inheritance polymorphism
add a comment |
I was wondering if it is frowned upon to have an empty function in a parent class for child classes to override if they needed it? I personally think the use case below justifies it -
class Foo
void performAdditionalChecksIfNeeded(String decrypted)
void validate(String encrypted)
final String decrypted = decryptIfProperlyEncrypted(encrypted);
performAdditionalChecksIfNeeded(decrypted);
class Bar extends Foo
@override
void performAdditionalChecksIfNeeded(String decrypted)
// Additional validation logic
I'm aware that Bar
class could've simply overridden validate()
and called super.validate()
to eliminate the need of the empty function in Foo
. The reason why I had the code arranged this way is that decryptIfProperlyEncrypted
uses a library that I wanted to keep only in the Foo
class due to separation of concern, performance concerns (decryption), and code duplication avoidance. In other words, if performAdditionalChecksIfNeeded
didn't exist in Foo
I'd have to perform the decryption again in Bar
.
java inheritance polymorphism
1
I don't see why it should be a problem. There are more functional alternatives, but an empty overridable method is a reasonable approach to some problems.
– khelwood
Mar 8 at 19:26
add a comment |
I was wondering if it is frowned upon to have an empty function in a parent class for child classes to override if they needed it? I personally think the use case below justifies it -
class Foo
void performAdditionalChecksIfNeeded(String decrypted)
void validate(String encrypted)
final String decrypted = decryptIfProperlyEncrypted(encrypted);
performAdditionalChecksIfNeeded(decrypted);
class Bar extends Foo
@override
void performAdditionalChecksIfNeeded(String decrypted)
// Additional validation logic
I'm aware that Bar
class could've simply overridden validate()
and called super.validate()
to eliminate the need of the empty function in Foo
. The reason why I had the code arranged this way is that decryptIfProperlyEncrypted
uses a library that I wanted to keep only in the Foo
class due to separation of concern, performance concerns (decryption), and code duplication avoidance. In other words, if performAdditionalChecksIfNeeded
didn't exist in Foo
I'd have to perform the decryption again in Bar
.
java inheritance polymorphism
I was wondering if it is frowned upon to have an empty function in a parent class for child classes to override if they needed it? I personally think the use case below justifies it -
class Foo
void performAdditionalChecksIfNeeded(String decrypted)
void validate(String encrypted)
final String decrypted = decryptIfProperlyEncrypted(encrypted);
performAdditionalChecksIfNeeded(decrypted);
class Bar extends Foo
@override
void performAdditionalChecksIfNeeded(String decrypted)
// Additional validation logic
I'm aware that Bar
class could've simply overridden validate()
and called super.validate()
to eliminate the need of the empty function in Foo
. The reason why I had the code arranged this way is that decryptIfProperlyEncrypted
uses a library that I wanted to keep only in the Foo
class due to separation of concern, performance concerns (decryption), and code duplication avoidance. In other words, if performAdditionalChecksIfNeeded
didn't exist in Foo
I'd have to perform the decryption again in Bar
.
java inheritance polymorphism
java inheritance polymorphism
asked Mar 8 at 19:22
ddolceddolce
338418
338418
1
I don't see why it should be a problem. There are more functional alternatives, but an empty overridable method is a reasonable approach to some problems.
– khelwood
Mar 8 at 19:26
add a comment |
1
I don't see why it should be a problem. There are more functional alternatives, but an empty overridable method is a reasonable approach to some problems.
– khelwood
Mar 8 at 19:26
1
1
I don't see why it should be a problem. There are more functional alternatives, but an empty overridable method is a reasonable approach to some problems.
– khelwood
Mar 8 at 19:26
I don't see why it should be a problem. There are more functional alternatives, but an empty overridable method is a reasonable approach to some problems.
– khelwood
Mar 8 at 19:26
add a comment |
1 Answer
1
active
oldest
votes
Why not use composition over inheritance in this case? The only thing Foo class seems to be doing is the decryption. I would use it in Bar class as a dependency.
The decryption method throws an exception if the encrypted string made no sense, in some of our use cases that is the only validation needed (to see if something is properly encrypted)
– ddolce
Mar 8 at 20:11
I don't see it as a validation, it is a decryption operation that can go wrong if the data isn't valid. This operation could be encapsulated inside a class and used where it is necessary.
– Diego Marin
Mar 8 at 20:18
add a comment |
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%2f55069689%2fjava-inheritance-empty-method-in-parent-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
Why not use composition over inheritance in this case? The only thing Foo class seems to be doing is the decryption. I would use it in Bar class as a dependency.
The decryption method throws an exception if the encrypted string made no sense, in some of our use cases that is the only validation needed (to see if something is properly encrypted)
– ddolce
Mar 8 at 20:11
I don't see it as a validation, it is a decryption operation that can go wrong if the data isn't valid. This operation could be encapsulated inside a class and used where it is necessary.
– Diego Marin
Mar 8 at 20:18
add a comment |
Why not use composition over inheritance in this case? The only thing Foo class seems to be doing is the decryption. I would use it in Bar class as a dependency.
The decryption method throws an exception if the encrypted string made no sense, in some of our use cases that is the only validation needed (to see if something is properly encrypted)
– ddolce
Mar 8 at 20:11
I don't see it as a validation, it is a decryption operation that can go wrong if the data isn't valid. This operation could be encapsulated inside a class and used where it is necessary.
– Diego Marin
Mar 8 at 20:18
add a comment |
Why not use composition over inheritance in this case? The only thing Foo class seems to be doing is the decryption. I would use it in Bar class as a dependency.
Why not use composition over inheritance in this case? The only thing Foo class seems to be doing is the decryption. I would use it in Bar class as a dependency.
answered Mar 8 at 19:30
Diego MarinDiego Marin
146111
146111
The decryption method throws an exception if the encrypted string made no sense, in some of our use cases that is the only validation needed (to see if something is properly encrypted)
– ddolce
Mar 8 at 20:11
I don't see it as a validation, it is a decryption operation that can go wrong if the data isn't valid. This operation could be encapsulated inside a class and used where it is necessary.
– Diego Marin
Mar 8 at 20:18
add a comment |
The decryption method throws an exception if the encrypted string made no sense, in some of our use cases that is the only validation needed (to see if something is properly encrypted)
– ddolce
Mar 8 at 20:11
I don't see it as a validation, it is a decryption operation that can go wrong if the data isn't valid. This operation could be encapsulated inside a class and used where it is necessary.
– Diego Marin
Mar 8 at 20:18
The decryption method throws an exception if the encrypted string made no sense, in some of our use cases that is the only validation needed (to see if something is properly encrypted)
– ddolce
Mar 8 at 20:11
The decryption method throws an exception if the encrypted string made no sense, in some of our use cases that is the only validation needed (to see if something is properly encrypted)
– ddolce
Mar 8 at 20:11
I don't see it as a validation, it is a decryption operation that can go wrong if the data isn't valid. This operation could be encapsulated inside a class and used where it is necessary.
– Diego Marin
Mar 8 at 20:18
I don't see it as a validation, it is a decryption operation that can go wrong if the data isn't valid. This operation could be encapsulated inside a class and used where it is necessary.
– Diego Marin
Mar 8 at 20:18
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.
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%2f55069689%2fjava-inheritance-empty-method-in-parent-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
1
I don't see why it should be a problem. There are more functional alternatives, but an empty overridable method is a reasonable approach to some problems.
– khelwood
Mar 8 at 19:26