d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java, Method Overriding
Add Reply New Topic New Poll
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Apr 14 2019 05:33pm
Does method overriding in java work with composition or only inheritance?
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Apr 16 2019 03:25pm
Method is overriden when it comes from extended class or implemented interface.
Composition is an entirely different thing, and it is somewhat an opposite of inheritance.
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Apr 16 2019 03:39pm
From past experience I don't think you'll read this, but it will help you to understand the question your're asking and the answer I'm going to give you: https://en.wikipedia.org/wiki/Composition_over_inheritance

I'd say a key part of composition is to avoid inheritance and thus overriding methods.
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Apr 16 2019 03:41pm
Quote (pzold @ Apr 16 2019 05:25pm)
Method is overriden when it comes from extended class or implemented interface.
Composition is an entirely different thing, and it is somewhat an opposite of inheritance.


Is it really considered method overriding if you're implementing an interface? I'd say not really.
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Apr 16 2019 03:59pm
Quote (waraholic @ Apr 16 2019 10:41pm)
Is it really considered method overriding if you're implementing an interface? I'd say not really.


You're right, my inaccuracy - I was referring to "override" not strictly as a verb, but meant the "@Override" annotation (in terms of language).
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Apr 16 2019 04:39pm
Quote (pzold @ Apr 16 2019 05:59pm)
You're right, my inaccuracy - I was referring to "override" not strictly as a verb, but meant the "@Override" annotation (in terms of language).


Yes so if you're thinking about just using the @Override method sure you'll use it in both scenarios, but I think the idea of composition is to not override methods of a class (but you must override methods of an interface and that is fine because you're only writing the implementation once).
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll