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,

CodePudding user response:

reference qybao reply: 3/f
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,


Interface is a simplified version of the functions can much inherited abstract class, I feel this is the case,
In addition, the type of single inheritance to avoid the problem of the parent class method with the same, but the interface of multiple inheritance method with the same way,

CodePudding user response:

1. Java classes (and abstract class) is a single inheritance (to a hierarchy), interface can realize multiple extension functions (well) at the same time,
2. An abstract class is generally designed to similar things in common, the interface is usually in order to uniform standard (can be not the same thing),

CodePudding user response:

Objects in object-oriented, thought to consider,

A class inherits A class B, B is A's father, all property inheritance B (in addition to B write will not let A succession, namely private), if you have any abstract methods in B, or B to A secret, A according to practice, practice what up to A,
In Java, an object can have only one father, (in c + + can have multiple dad, such as father, stepfather, andie, make complex)

A class A implement one interface, C of C is A teacher, he learned some martial arts from the C (implementation), as for after he learned how to practice, is also A to do your own thing,
A class can have multiple master, from different places to learn different skills,



In practical applications, why there are interface? It is clear to tell you, there are not allowed to have achieved, only allowed definition, and the interface is to be made public, written doc document, and generally do not made public, in addition to the tools, such as Java will connect to the database, such as Mysql, Oracle, Redis, directing, these are the different vendors, Java can not link them one by one to write code, procedures, and the manufacturer's code won't open, don't know how or why the Java connection,

How to do? Java will define an interface, general connection database interface, if a database to support Java, is by the database vendor to define a class, the realization of the interface, and Java won't go to tube specific code realization, if using abstract classes, Java in one thousand added a loophole attack code, the manufacturer also dare to use?



In actual projects, the interface specification, said after the start of the project, project division of labor, will be defined by the architect or senior engineer project module interface, framework, and then by the junior engineers (yards) to as interface to realize these methods, this means that the architect can't write ordinary simple business code, just define the core functionality framework interface and write code,
If an abstract class, it is not made public, even open the others also don't understand this abstract class in addition to the public interface, has done, in addition business programmers will also confused


In conclusion, the interface is to define the specification, the public, abstract class implements some functions, some functions will not be possible because business complex or not sure how to implement, and not made public,

CodePudding user response:

Interface is a protocol that can do what the object, an abstract class is a "must implement", illustrates the object "rough" is how to do those things,
Compared with interface and an abstract class, focused more on implementation
Interchangeable between both, in certain situations, but the moment does not exist the possibility of who can entirely replace who

CodePudding user response:

reference 4 floor qq_16774199 response:
interface is a simplified version of the functions can more inherited abstract class, I feel so,
In addition, the type of single inheritance to avoid the problem of the parent class method with the same, but the interface of multiple inheritance method, with the same


Interfaces are abstract methods had no substance, so all depends on the subclass implementation, method of the same name will not conflict,

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related