aspose file tools
The moose likes Beginning Java and the fly likes why i need interface and abstract class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "why i need interface and abstract class ?" Watch "why i need interface and abstract class ?" New topic
Author

why i need interface and abstract class ?

Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
hi, why we need interface ? it only contains the method definition with no method body , then why a class want to implement it ?, since i have to re-write my method definition with method body in my new class again.
and also to abstract class , even it allow us to extends defined methods , i can just simply put all the method into my new class .
what are the advantages as we do that ? because the code readability ? please enlight me ....
tq
Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
I'll start with the interfaces.
Interfaces are used to hide the implementation of a class. For eg if I write a class that sends out an email if you give it a certain text and ppl want to use that class, they only need to know which methods there are, not how they are implemented. So they use the interface, if (in a next version) the class changes from plain text email to html emails, I can keep the interface the same, make some extra private methods and the user doesn't have to change anything to his program...
For abstract classes:
Eg take a motorised vehicle abstract class. Some items are in common for all motorised vehicles, they all have an engine and a nr of cilinders, but there doesn't exists a 'Motorised Vehicle', there only exists things like: Car, Motorbike,...
So they all share some methods, but you shouldn't be able the instanciate a 'Motorised Vehicle'..
Hope I could clear some things up for you...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

You might want to read How my dog learned polymorphism.


[Jess in Action][AskingGoodQuestions]
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
so, interface is not for reuse ?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: why i need interface and abstract class ?
 
Similar Threads
Need of interface
diff b/w abstract classes and Interfaces
interface is abstract?
Abstract definition & Interface
Static Methods in an Interface