HI, Abstract class and interfaces both we have to declare methods. at what situation we can use abstract class and interfaces. both should have to use declared methods. but why mostof the time we prefer interfaces? why not abtstract class.
Java does not permit multiple inheritance primarily because the designers of the language didn't want it. It it messy to implement and can cause a fair number of errors from programmers who attempt to use it.
It is possible to simulate it, through creative uses of interfaces. Consider:
Also, please read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you. [ January 05, 2005: Message edited by: Joel McNary ]
Whilst interfaces can be used to simulate multiple inheritance (in the C++ sense) that's a mis-use and a bit of a red herring. Interfaces are not there to give you multiple inheritance in Java: if you're doing that then either there's something wrong with your design, the design of some class library or the way you're trying to use it.
To answer the thread starter's question: why use an abstract class instead of an interface (and vice versa)?
I would *always* use an interface, as that defines the set of operations supported by my classes. I might then consider implementing that interface as an abstract class in order to provide some skeleton code or some default behaviour.
Why would I use both?
Because by implementing the top level as an interface I allow for the possibility of anything being overridden: I don't impose any unnecessary restrictions on how the interface is implemented in the future.
I'd use the Abstract class to implement any common implementation: and I'd make it abstract even if I implemented every method defined in the interface because (without going into the reasoning) only the leaf classes of an inheritance hierarchy should be concrete. I wouldn't necessarily have an abstract class at all.
Hope that helps.
Edward [ January 07, 2005: Message edited by: Edward Kenworthy ]
why and when we use interface instead of abstract,here my doubt is abstract is more advantage than the interface,we can create abstract methods and somemethod will be left unimplemented,so all point of view abstract is more advantage.but why should we use interface and when we use?recently i faced this question in interview
sekhar kiran wrote:why and when we use interface instead of abstract,here my doubt is abstract is more advantage than the interface,we can create abstract methods and somemethod will be left unimplemented,so all point of view abstract is more advantage.but why should we use interface and when we use?recently i faced this question in interview
sekhar kiran wrote:why and when we use interface instead of abstract,here my doubt is abstract is more advantage than the interface,we can create abstract methods and somemethod will be left unimplemented,so all point of view abstract is more advantage.but why should we use interface and when we use?recently i faced this question in interview
i explained in interview as the same which mentioned above the link,but he asked ie what are the features in interface will be support in abstract class also.eg interface having public and abstract where it will be available in abstract.so totally abstract is more advantage than interface,then why we use interface?i dont know how to answer to them
sekhar kiran wrote: why we use interface?i dont know how to answer to them
If you read this thread again and the link, I am sure you will get your answer. For instance, Winston replied you with these reasons,
Interfaces allow multiple inheritance.
They allow you to define a type (or, if you prefer, a contract) without any implementation.
They allow you to put off decisions that don't need to be made (see the WhatNotHow (←click) page for details - 2nd half).
I also recommend you to search about this question in this forum and you will find a lot of similar threads discussing very valuable points.
sekhar kiran wrote:so totally abstract is more advantage than interface
Then I suggest you start designing a few applications using totally abstract classes.
Hey, you may be able to get them to work...maybe even well. But hopefully, at some point, you'll run into a big problem and a light-bulb will come on - and then maybe you'll be able to come back to this thread when you're in the mood to listen and do some research of your own.
Winston
Post by:autobot
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking