Abstract / Interface | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Abstract / Interface

Can someone explain me, when should i use abstract / interface ? abstract and interface are use for implent method on drived class right? I'm still cnfuse when should i use abstract / interface.

20th Nov 2017, 2:24 PM
Sonny Michael
Sonny Michael - avatar
5 Answers
+ 5
Interfaces are used when we want unrelated classes to implement a set of methods in common, usually in different ways. For example, a radio control allows a limited number of operations: changing stations, adjusting volume ... So different radios can implement different controls (interfaces) in different ways. When implementing the interface you can not tell what to do, but how to do it. The interface specifies what operations the radio should do but not specify how to do it, it will be done according to each radio that implements the interface. public interface Radio{ void adjusteVolume(); } public class SamsumgRadio implements Radio{ . . @Override public void adjusteVolume(){ . . way the Samsung radio adjusts the volume } } public class LGRadio implements Radio{ . . . @Override public void adjusteVolume(){ . . way the LG radio adjusts the volume } }
21st Nov 2017, 2:34 AM
Malkon F
Malkon F - avatar
+ 2
but I explained when they were used and @Kavya also explained. The links above also explains."Interfaces are used when we want...." read again bro
21st Nov 2017, 4:20 PM
Malkon F
Malkon F - avatar
0
If you want to use multiple inheritance use interface , because you cannot extend two abstract classes at same time. With interfaces you have ability to do it.
21st Nov 2017, 1:33 AM
Kavya Sri
Kavya Sri - avatar
0
actually i know what is interface, Sorry.. my question is when should i use between intrface and abstract?
21st Nov 2017, 9:20 AM
Sonny Michael
Sonny Michael - avatar