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










0















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.










share|improve this question

















  • 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
















0















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.










share|improve this question

















  • 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














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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













  • 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













1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























  • 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











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
);



);













draft saved

draft discarded


















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









0














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.






share|improve this answer























  • 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















0














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.






share|improve this answer























  • 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













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















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.




draft saved


draft discarded














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





















































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

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme