#Why is interface needed in this example?
1 messages · Page 1 of 1 (latest)
<@&987246841693360200> please have a look, thanks.
Any help?
Why do you think it's not needed?
Maybe its good to go back to the roots and understand the concept of interfaces ^^ Understanding interfaces as a method contract, and a way to allow different implementations of a method. Which allows a class to define that they promise to provide the specific behavior promised by the interface. In this way all implementing classes can be treated the same.
You could try to implement the same code as you've shown in the picture without interaces and with interfaces, if you'd like to feel the difference.
You can read these if you want to see some more explanations and examples
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
https://java-programming.mooc.fi/part-9/2-interface
https://en.wikipedia.org/wiki/Interface_(Java)
This beginner Java tutorial describes fundamentals of programming in the Java programming language
Helsingin yliopiston kaikille avoin ja ilmainen ohjelmoinnin perusteet opettava verkkokurssi. Kurssilla perehdytään nykyaikaisen ohjelmoinnin perusideoihin sekä ohjelmoinnissa käytettävien työvälineiden lisäksi algoritmien laatimiseen. Kurssille osallistuminen ei vaadi ennakkotietoja ohjelmoinnista.
An interface in the Java programming language is an abstract type that is used to declare a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final)...
In the above screenshot, the interface allows any of those 3 implementations to be used interchangeably.
If they don't need to be used interchangeably, then you might not need the interface.
Side note, I don't think the open-closed principle very helpful in most situations.
It's quite an abstract concept, and on its own lacks context of when it's useful and when it's harmful.
If you find it helpful, that's great, don't let me minimize that for you. But most of the time I see it causing confusion, and so if you're confused about it, you're not alone!
Open and closed principle tells Open for extension and closed for modification
FOR EXAMPLE -
think of it like u have a laptop it has a track pad for mouse what if u wanted a mouse instead of trackpad
will u disassemble the whole laptop just to connect it to mouse
Nope, Instead you have USB port where u plug things in so that ur new mouse works
YOUR CODE
- FIRST LET US SEE WHAT IF WE DIDNT HAVE ANY INTERFACE
Imagine you have a class which is named as YoutubeIncomeEarnings
You will have to do something like this
public class YoutubeIncomeEarnings {
public double calculateEarnings(String category, Video video) {
double earnings = 0.0;
if (category.equals("Educational")) {
earnings = video.getLikes() * 0.013 + video.getViews() * 0.0013;
} else if (category.equals("Gaming")) {
earnings = video.getLikes() * 0.012 + video.getViews() * 0.0012;
}
return earnings;
}
}
now what if u wanted to add education and show how much income they should get or some more channel
you will have to add another elseif inside that class
you will have to manually change the code base
just disassembling laptop and adding part and then again assembling it just to add mouse instead of just of how we just used USB
+BEST WAY BY USING OPEN CLOSE PRINCIPLE
make an interface called as IEarningsCalculator
and implement this interface to whatever new class you want and pass the object in YoutubeEarnings just like this ur image
and in main class you will do something like this
EarningsCalculator educationalCalculator = new EducationalEarningsCalculator()
double educationalEarnings = earningsService.calculateEarnings(educationalCalculator, video);
HOPE THIS HELPED PLEASE DO LET ME KNOW
Detected code, here are some useful tools:
think of it like u have a laptop it has a track padfor mouse whatif u wanted a mouse instead of trackpad will u disassemble the whole laptop just to connect it to mouse Nope, Instead you have USB port where u plug things in so that urnew mouse works
@latent nacelle
man that took soo long to type
am trying to improve my explanation skills please do rate me at end whoever reads
@vivid robin how is my explanation 
public double calculateEarnings(String category, Video video) {
double earnings = 0.0;
if (category.equals("Educational")) {
earnings = video.getLikes() * 0.013 + video.getViews() * 0.0013;
} else if (category.equals("Gaming")) {
earnings = video.getLikes() * 0.012 + video.getViews() * 0.0012;
}
return earnings;
}
}
now what if u wanted to add education and show how much income they should get or some more channel
you will have to add another elseif inside that class
you will have to manually change the code base
just disassembling laptop and adding part and then again assembling it just to add mouse instead of just of how we just used USB```
Can't u just extend YoutubeIncomeEarnings
i had to go from startso that he knows why
No just the way u explained it sums to interface = open closed princp
Just thought I should give some constructive feedback
xd sorry to say but the example u choose is terrible
I am ngl this earnings example is more about inheritance, defining contract between classes and re-useability
This was very difficult to read on mobile.
- large headings where smaller headings would have worked just as well
- using code blocks for descriptions,
- not using code blocks for code
- using all caps
- using screenshots of code
good effort tho
yeah effort 10/10, I appreciate the intention put into trying to format things well, it just wasn't accessible on mobile.
so I can't easily rate the content itself yet.
oh thx
i will improve
i tried to tell why are we using OCP
by telling the issue first then solving
Today's popular version of open-closed is not too far off from "inheritance, defining contracts between classes and re-usability"
https://en.wikipedia.org/wiki/Open–closed_principle#Polymorphic_open–closed_principle
What would you add or change?
yea i didnt specify interface i was trying to tell how can we code to do extension and not modification
i was not emphasizing on interface his code had so was trying to relate
I suppose it is all heavily related
As someone who has studied software design enough, I find open-closed to be true, but not necessarily helpful. This above confusion is a prime example of it not being helpful.
It's application often includes debates about exactly what it means, and offers little guidance about when it applies and when it doesn't apply, leading to frequent misapplication.
Also, just because good software often exhibits open-closed, does not mean that making your software open-closed will make it good. This also makes it unhelpful in my experience, especially unhelpful as a recommendation for someone beginning to improve their design skills. Pursuing open-closed for the sake of open-closed itself usually leads to lower quality results.
yo i was about to say it
cos even i am learning and trying to have all SOLID makes code more confusing and large
but slowly ig i will learn