Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialYashu Jain
518 PointsJava Interfaces and Abstract Classes
When to use interfaces and abstract classes in java?
1 Answer
Parth Shah
Courses Plus Student 1,941 PointsHello ,I too am pursuing this course on Data Structure on Java.I too had the same question on what is the difference beetween an abstract class and an Interface.I too am a beginer so don't just trust me and right me if am wrong.
First of all I would like to point out the difference beetween Interface and abstract class.Interface as you see is just signature of the methods and not the method itself.e.g.
interface MotorVehicle { void run();
int getFuel();
} class Car implements MotorVehicle {
int fuel;
void run()
{
print("Wrroooooooom");
}
int getFuel()
{
return this.fuel;
}
}
And an Abstract Class looks more like this
abstract class MotorVehicle {
int fuel;
int getFuel()
{
return this.fuel;
}
abstract void run();
}
class Car extends MotorVehicle { void run() { print("Wrroooooooom"); } }
So you see the difference in the syntax.You would be thinking so why do we use interface as in the first example we could just have deleted the interface and just written class Car it would have totally worked then too. Yaa you are right it would have worked but you see that's not the reason of interface, we use interface so that we could use it on different objects who have only difference in their methods but after creating that methods you could use all their methods invariably as if there is no difference.For e.g.,
Say an Organization is organizing a Car Race and organizes many companies for it.They will write the code for the race()(provide them with drivers) and companies just have to submit their cars to the race which can run(), applyBrake(),turn() e.t.c. but they need to make sure they all submit just the things allowed in the race which their drivers knows how to use so they sign a contract where they list the things that the company can do now it's upon them on which engine to use,which rubber to use for their tires and all of that.Then the companies make their product(code) and submit to the Race Organizers.The Race Organizers make their code on what all things the car should run(),turn(),applyBrake() and all to win the race,So the Organizers didn't need to use different piece of code for different cars all they nedded to do was to sign a contract with those who participated.So that's the advantage of an Interface over Abstract Class. You see in this video how after just adding an Interface Comparable we could simply use Sort function on treets that's because someone already wrote the sort function using method compareTo() we here just need to change that method and it will sort automatically.
If I made any mistakes please correct me.
Thanks
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsAlexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsQuestion you are asking is not clear. The chances high no one will answer. sorry.
Try to reformat question to make it more concrete. Or simply google. There is a lot of tutorials, examples about abstract classes and interfaces out there.
Also if follow along Java Web Dev Track, you'll see a ton of examples in Web Applications.