Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interface Vs Abstract Classes

Status
Not open for further replies.

bsuribabu

Programmer
Apr 4, 2002
25
IN
Hi,

Can u pls explain me what are the diffs between
Interface and Abstract Classes


Suri
 
You implement an inferace. This means if an interface has ten methods defined you have to implement them all. The interface only defines the method names paramaters exceptions they throw and return types. It is up to you to rite that codes that adheres to that inerface. an interface is kinda like a contract and the methods of it clauses of that contract. An abstract class is a class that must be extended for it to be used. This class has methods which are already written for you. When you extend the class these methods come available for you to use. The reason it is called abstract is that it makes no sense to instantiate it on its own.
 
I don't have a direct answer to differentiate abstract class from interface other than a class can implement an interface but it cannot extend it. Only an interface can extend another interface. In the same regard, An interface cannot implement an interface of class.

An abstract class can never be instantiated. Its sole purpose is to be extended. An abstract class can extend another abstract class; BUT it does not have to implement them. However, the first concrete class that extends the abstract class HAS to provide the implementations to all the abstract methods.
 
oppss.. change the line "interface cannot implement an interface of class" to "interface cannot implement an interface OR class"
 
Hi,

Can u explain what basic difference between doGet and doPost requests


Suri
 
doGet and doPost are not requests--they are methods in the HttpServlet class to handle these two HTTP request types.

The basic difference is, form parameters are sent in a GET as part of the request URL. In a POST, they are sent in the request body.
 
Another difference between interfaces and abstract classes:
a Class CAN implements more interfaces but can extend ONLY 1 abstract class.(i.e. no polymorphism allowed in java)

An abstract class is usefull when you want that sub-classes can use some method you wrote once, an interface when you want that all the classes that implements the interface has some method.
For example take a JForm with a lot of JTextField inside where you put different kind of data. You can create an interface with a method call getValue and a method call setValue and than you can create some class, that implements the interface, for the variuos kind of data that you expect... one for numerical value, one for currency, one for datetime, and so on. If then you put in the classes a method to validate the user input and this method is called before returning the value with the getValue you won't have to check the value anymore.

HTH.



Leonardo Mattioli
Chief Program Manager
Z Forge snc -
 
polymorphism is allowed in java. perhaps you meant multiple inheritance is not allowed in java?

imho, getvalue and setvalue(public accessor methods) should be done inside the class itself . I never use them in an interface because 1. I have one less interface to implement; if needed. 2. to make it more readable, the methods should be implemented inside the class to allow outsiders to get to the private methods. it would be awkward to declare all these private variables in one class and define all the public accessors for these variables in an interface. I believe loosely coupled is good only to some extend.

just a thought...
 
Yes, Maxpower1, you're right and I'm wrong... polymorphism is allowed... i meant multiple inheritance... sorry.
For what concern getValue and setValue I've used something like that in some program.

In the program I've specified some different JPanel with inside a JLabel, an input field and some JButton (for help and for search)... I've created 5 kind of JPanel (one for datetime, one for Currency, one for numeric values, one for a yes/no response and so on) and put all the MyJPanels in an array inside a JForm... so when I've to read or write a value I don't matter about the kind of the JPanel but simply I cast the JPanel to my interface and use the get and set method... It was more simple to do the program because I was able to switch between a MyJPanel to another when my customer change idea about some aspect of a JForm.

IMHO was the perfect solution because I worked with widgets with a upper level of abstraction instead using the Java ones... also because I had 7 JForm with a total of 130 different questions for the end user; so I had 130 MyJPanel instead 130 JLabel and 130 JTextField and 300 JButton, etc...

Bye.

Leonardo Mattioli
Chief Program Manager
Z Forge snc -
 
Just to add, take a look at the ActionListener interface. This has it's own methods and variables. However, in order to use it, you must define the actionPerformed(ActionEvent) method.

-Greg :-Q

flaga.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top