0
please anyone can clarify the concept of abstract class and interfaces in detail plz
4 odpowiedzi
+ 3
Its more of an issue of design, you create abstract classes because you will create classes based on their model (like various animal species based on abstract animal) while you create interfaces for API
Ex: you have interfaces birdlikemovement, predatoryfeedingstyle, quatrupedmovement - then for wolf class you implement the interfaces quatrupedmovement, predatoryfeedingstyle and for eagle the interfaces birdlikemovement and predatoryfeedingstyle.
Continuing the example, for animals that implement birdlikemovement you have to code the interface's method FlyFromAtoB() for animals that implement predatoryfeedingstyle you have to implement the method FeedOnOtherAninals() and the only animals that are allowed to go over a rift are the animals that implement the birdlikemovement (because they can fly).
It's kinda like a contract, you declare that it will behave this way but you also have to code the "this way" behavior.
Not a great example but I hope it helps you understand.
+ 2
abstract means you will define that function in your inheritance class and you can also define in abstract class that is mother class.
Interface is 100% abstract class, it means you have to define your function in inheritance or daughter class. For more comment below
+ 2
An abstract class is a class that you cannot instanciate. You define abstract methods in it in order to say to their daughter classes that they HAVE to define them.
An interface is different from an abstract class as it is not a class per say, it doesn't have a constructor. Moreover, in an abstract class, every method has to be abstract, this is not true for the interface.
Interfaces are a good way to do multi-hierarchy, say for example you have a class Rectangle and a class Square that extends Rectangle, but you also want to say that a Square can by a rhombus, you can define an interface implemented by the Square class so that the square inherits methods from both the rectangle and the rhombus.
0
Thanks a lot all of you honourable people i really cleared my concept