Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the defination of the abstract classes and methods say the methods do not have no body and is just a simple name; why do not simply put the interface aside and use an ordinary method definition in the code?

What I have tried:

because the interface doesn't apear to reduce any code length or making anything easier in the programming
Posted
Updated 20-Mar-22 5:54am

If you want to learn about a programming language then the best place is the official documentation: Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language)[^].
 
Share this answer
 
Quote:
the interface doesn't apear to reduce any code length or making anything easier in the programming

That's either because you don't understand it, or you just aren't experienced enough yet.

An interface isn't a class at all - it just looks like one. What it is is a promise, or a contract: "if you do this thing, I'll let you join my club" And joining the club gives you advantages that all members share, and non members don't.
For example, you use foreach in your code, yes? That works because each of the collections you can use with foreach implement the Iterable interface[^] - it you don't implement it, you can't iterate over it! If your class implements Iterable, it "joins the club" and an instance of your class can be used as a target in a foreach. If it doesn't, the compiler "door staff" / "bouncers" won't let it in! :laugh:

That's an obvious example, but there are many, many places where Interfaces make a developers life easier, just as Inheritance itself does. As Richard suggests - read the documentation!
 
Share this answer
 
Although I agree with Richard and Griff, I must add a critical note:
Too many times I have seen needless interface implementations from people that think that every class must have an interface.
See article here: Best Practices in Software Development: Interface Overuse - DZone Java[^]
 
Share this answer
 
Comments
Richard MacCutchan 20-Mar-22 10:47am    
I agree, but it's not limited to Interfaces. Far too many new developers (all found here) think they can learn by copy and paste.
Interfaces and abstract classes are "contracts." If you fail to implement everything spelled out in those "contracts" in your code that uses those interfaces and classes, your code doesn't compile.

It's both something that the code you're writing against expects of your code, and a handy reminder to implement everything spelled out in those contracts.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900