Home > Back-end > Can completely replace the interface technology on abstract class?
Can completely replace the interface technology on abstract class?
Time:09-25
Interface can do, can do an abstract class, difference is only in the aspect of grammar, so what is the meaning of the interface? Just for the sake of taking care of the complexity of reality? People and dogs can be mobile, belong to animals, so move speed write abstract class inside, And the aircraft can be mobile, belong to the animals, the plane belongs to the mechanical, so move speed wrote interface,
And this is the abstract class way:
Public class test {
Public static void main (String [] args) { Horse myhorse=new horse (); Myhorse. Eat (); Myhorse. Travel (); System. The out. Println (Animal. The getValue ()); System. The out. Println (Animal. The getDouble ()); System. The out. Println (myhorse instanceof horse); System. The out. Println (myhorse instanceof Animal); }
}
The class horse extends Animal {
@ Override Public void eat () { System. The out. Println (" to eat "); }
@ Override Public void travel () { System. The out. Println (" to travel "); } }
Public abstract class Animal {
Public abstract void eat ();
Public abstract void travel ();
Public static int getValue () { The return of 11; }
Public static double getDouble () { The return of 0.92; }
}
And this is the interface way:
Public class test {
Public static void main (String [] args) { Horse myhorse=new horse (); Myhorse. Eat (); Myhorse. Travel (); System. The out. Println (myhorse thedefault ()); System. The out. Println (Animal. The getValue ()); System. The out. Println (Animal. The getDouble ()); System. The out. Println (myhorse instanceof horse); System. The out. Println (myhorse instanceof Animal); }
}
The class horse implements Animal { Public void eat () { System. The out. Println (" eat "); } Public void travel () { System. The out. Println (" travel "); } }
Public abstract interface Animal {
Public abstract void eat (); Public abstract void travel (); Public default String thedefault () { Return "interface provides the default method"; }
Public static int getValue () { Return 10; } Public static double getDouble () { The return of 0.98; } }
CodePudding user response:
Implement but class can inherit an abstract class can implement multiple interfaces, the interface is easy to extend
CodePudding user response:
This is a profound problem you
Not sure how the original abstract classes and interfaces, the following is my guess,
During the period of c + + classes can be multiple inheritance, but multiple inheritance also brings many problems, various scopes, visit chaos
So Java is changed to a single inheritance, but single inheritance has also led to some things cannot be integrated together, like a printer, and a copier, a printer, copy machine to come trouble, make uncertain, and thus offering out of the interface, to make multiple inheritance,
Look from the above example, it seems that the interface is the ultimate direction, may replace abstract classes, like many frameworks, like OSGI, each bundle is published interface to go out, for external use,
The world is very complex, may not be abstract classes and interfaces can be solved, maybe just a heap of accumulation of methods, may be in the computer world still pregnant bursa another thing, When he came out, abstract classes and interfaces will be crushed,
CodePudding user response:
Can't replace, Java is a single inheritance, interface can implement multiple, at the same time, of course, also can declare an abstract class and interface the same way, but result in abstract classes are bloated, not flexible, high coupling, is not conducive to expand, Likewise, the interface can replace the abstract class, although java8 later interface can define defalt method, but also does not support the static properties, and the interface methods are public, not don't want to expose hidden details, to do less than an abstract class can do some of the functionality,