Difference abstract class interface?

What is the difference abstract class interface Java? And in what situations is better to use abstract class and when interface?
July 2nd 19 at 17:11
5 answers
July 2nd 19 at 17:13
What is the difference abstract class interface Java?

All depends on the concept of "type". In the old days, that is, during the language Simula, which was inspired by the creators of C++ were just classes. And class-based type system. Moreover, the mechanism of inheritance was carried out as implemented, only to save memory, which in those days was very expensive.

To achieve polymorphism, we must have the ability to declare abstract types. They say "any crap which is of such a type will work as it should". Because in languages like C++ abstract classes appeared. Because sometimes we want to make a composition of abstract types in C++ implemented multiple inheritance.

In Java, which largely drew inspiration from C++ and smalltalk, decided to introduce one more entity interfaces. It was sort of a simplified way to define an abstract base type. In the end not to solve the problem of diamond (or rhombus) of multiple inheritance was abandoned and to allow the classes to implementing multiple interfaces.

Due to this we are able to do composition types as we want. That is the whole difference boils down to the fact that when you inherit from an abstract class, we are inheriting the classes, while interfaces allow our classes to implementing abstract types.

In General, abstract classes need when you need inheritance. This is usually in situations where you have several classes that should have a common abstract type (i.e. you can't select the weakest on the limitations of the ancestor). For example, if we make a chain of class String <- Email, then there is no point in abstract classes since the String already includes a subset of the types of Email.

In General in Java 8 at already introduced interfaces have a default implementation so don't be surprised if over time from the keyword extends, in principle, refuse, getting rid of unnecessary entities.

So I recommend you to read: www.javaworld.com/article/2073649/core-java/why-ex...
Mnogabukav and it's not on - blake_Eichma commented on July 2nd 19 at 17:16
About the administrator user and not very good example. No need to teach bad. When I itself on this rake came when I wrote a chat with the administration. The best policy - the classic system role. Where in the model the role of specific field goes. No inheritance in this case is not necessary. And then what will you do in case if you need moderators, supermoderator etc for each class? :D - kurt_Jacobson commented on July 2nd 19 at 17:19
: here is a brief:

all depends on the types. Each object has a type. Interface = simple type, abstract class type + realization. It's pretty simple. What to use depends on what you need in a type hierarchy. If you have a need for in the underlying implementation (General rules) we choose an abstract class. In other cases when you need to determine the base type of the interfaces.

Inheritance is a harmful thing and it is better to avoid.

: Yeah, bad example. Just can't do it immediately, in principle, to come up with an example of inheritance with abstract class. Perhaps it was necessary that the thing with the chat rooms to paint instead. And I agree, a better object for separate roles. - Allene_Crona78 commented on July 2nd 19 at 17:22
: well I about the same wrote - blake_Eichma commented on July 2nd 19 at 17:25
: rewrote "a lot of beeches" in the other "a lot of beeches". - Allene_Crona78 commented on July 2nd 19 at 17:28
: not true. All in the subject. - jed.Mitchell commented on July 2nd 19 at 17:31
: it was not the topic. - blake_Eichma commented on July 2nd 19 at 17:34
: this is the rewritten version. I then brought sensitive. - Allene_Crona78 commented on July 2nd 19 at 17:37
: : well, then clear. - jed.Mitchell commented on July 2nd 19 at 17:40
July 2nd 19 at 17:15
To put it "simply" Wikipedia, the main difference between abstract class and an interface is that interfaces can inherit multiple, abstract classes can contain abstract things.

As for their use... it's all much easier if you understand why we need abstract class and why interface. Abstract class is used when we want to obtain a specific implementation, but it must be flexible. The interface is used to implement this thing actually was.
perhaps better suited "object can implementing a lot of interfaces", after all it's not really inheritance. Then the object with multiple interfaces can be interpreted as the composition types. - blake_Eichma commented on July 2nd 19 at 17:18
: well, well. In General, correctly says "the class promises to provide implementations of all the methods of neograniceno number of interfaces". The funny thing is that if I remember correctly, determine the implementation needs of all the methods in the case of interface, and in the case of abstractum class - optional. - kurt_Jacobson commented on July 2nd 19 at 17:21
: not necessarily only for abstract classes. Well, that is if we have one abstract class and inherit from another abstract class - there is not necessarily. For the regular classes of behavior generally the same.

I have a suspicion that "difference" is clearly visible under the kopotom. Went to Google, was already interested. Especially in light of the default implementations of methods in interfaces. - Allene_Crona78 commented on July 2nd 19 at 17:24
the difference is visible not only under the hood. Even on the surface shows that the interface is "just" a means to prove that the class meets singature. An abstract class is a class with virtual functions with no implementation by default. I.e. an abstract function with a virtual access, and the function of the interface - as usual - blake_Eichma commented on July 2nd 19 at 17:27
: well, if so then the difference is not so visible. Definitely not... now "knows better" that the implementation of "default". - Allene_Crona78 commented on July 2nd 19 at 17:30
: well, Yes. If there is no implementation, will be called by the default implementation of the interface. Virtual access will not be. Although, of course, very similar. And variables in the interface to declare the impossible, if I am not mistaken. - jed.Mitchell commented on July 2nd 19 at 17:33
: Yes, the interface may not be state. - blake_Eichma commented on July 2nd 19 at 17:36
July 2nd 19 at 17:17
Interfaces are used for multiple inheritance. The interface can not prescribe implementation methods. These restrictions, we exclude one of the problems with multiple inheritance: if we inherit from 2 classes with the same method (this is in C++ possible), then calling this method in the heir, it is unclear what implementation to use. Nasledujuci from multiple interfaces, we write an implementation specifically for our class and this problem is eliminated.
In Java 8 introduces the ability to implement default methods in interfaces. - blake_Eichma commented on July 2nd 19 at 17:20
: Interesting. I, at the time, presented the interfaces in this way. - kurt_Jacobson commented on July 2nd 19 at 17:23
July 2nd 19 at 17:19
Everything is there in Wikipedia:
The interface is just a pure abstract class, i.e. a class that is not defined nothing but abstract methods.
July 2nd 19 at 17:21
The abstract class contains abstract methods with General functionality, which is logical for the methods of his heirs(see towards the principle of substitution Barbara Lisko). The interface simply describes the capabilities.

Find more questions by tags OOPJava