answersLogoWhite

0


Best Answer

Abstract class provides a way of "being a [something like me]", or inheritance

interface provides a set of functionality (contract) as "behaving".

Abstract class provides the single inheritance and perhaps some default implementation, while interface may be implemented by different classes that have nothing to do one and other except the common interface implementation.

The preference I would start with:

Ask yourself that an object should be "Is a something or behave like something". If your answer is "Is a", then abstract class is more likely your good choice. But if your answer is behave like, does not need to Is a, then the interface is the way to go.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Java is object oriented, so you should use interface to encapsulate the behavior of an object. A specific class that implements the interface then encapsulates data and behavior. The abstract class is about encapsulating some part of your class in a base class, but you expect the derived classes to complete the rest of the implementation. This is when you declare an abstract method forcing the derived classes to implement that method. Note that both abstract and concrete methods can still be coming from an interface. Since the complete functionality is only available via a derived class, abstract classes cannot be dirctly instantiated.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When do you give preference to abstract classs and interface?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is difference between abstract class and interface in core java give brief answer with example?

Differences:Abstract class can also contain method definitions but an interface can contain only declarationsAll variables in an interface are by default public static and final whereas in Abstract class it is notAn interface can be considered as a pure abstract class that contains no method implementations and contains only declarations.


Can anyone 1 pls give a real time example and usage of abstract class and interface in c sharp?

Abstract classes and Interfaces are both examples of contracts with no default implementation. Interfaces are more readily interchangeable between applications (because they can be interpreted as jump tables). Because of this COM in Windows makes heavy usage of interfaces with several master ones being the source of all functionality (e.g., IUnknown). If a programmer wanted to define a type of thing (e.g., MusicalInstrument), that had certain abilities (e.g. Play) without defining how a MusicalInstrument plays... This could be done with an Interface or an Abstract class. If the programmer wanted to provide some base functionality to whomever was going to implement this functionality (e.g., Vloume, Output Stream) then this would be more appropriate for an Abstract class. Abstract classes can even build on interfaces: public interface IMusicalInstrument { void Play(); } public abstract class MusicalInstrument:IMusicalInstrument { private int vol = 10; public virtual SetVolume(int newVol){vol = newVol;} public abstract void Play(); } public class Horn:MusicalInstrument { public override void Play(){//* Play something *//} }


Explain the concept of interfaces in java with a suitable example for the same?

interface works like an abstract class it has methods which are not defined its job is to give a general idea about the class(generic) the class can have its own functions but interface makes java program to include all the functions or methods present in the interface there are ibuilt interfaces like runnable example in threads it is runnable it makes compulsory to the class to include all the methods in interface if you dont want that then use abstract key word before class eg: interface a{void meth(int i);} class one implements a{ void meth(int i) System.out.print(i); //all other members of class// if dont use meth function it will give an error for big projects certain general functions are necessary which have to be there everywhere it gives general idea of what the program is doing


Can abstract class have constructors?

A constructor of a class in invoked when a object of that class is created. As an abstract class can't have an object, so we can't create a constructor of the abstract class. But we can create a constructor of a concrete subclass of that abstract class and we have to pass the object of that concrete subclass to the abstract class.


Why httpservlet class called abstract?

Because, the creators of the servlet framework cannot give all the functionality that you might want in your application. So, if they make their class abstract, you the developer will be providing all the functionality you need and still stick to the framework standards defined by them.

Related questions

Why instance in abstract or interface?

we can make object of interface but in abstract we can not make object of it interface ab= new Classs(): in interface we maintain multiple inhetence by use of obj of interface we if inherit two class have same fun then we give the name of that interface and call the pertucular that fun interface ab= new class() ab.add(); but in Astract Class we cannot make object of it only class class wich inherit it can make object class ab2= new Class(); and by obj we call function of drived class ob2.add();


What is difference between abstract class and interface in core java give brief answer with example?

Differences:Abstract class can also contain method definitions but an interface can contain only declarationsAll variables in an interface are by default public static and final whereas in Abstract class it is notAn interface can be considered as a pure abstract class that contains no method implementations and contains only declarations.


Give you some examples of abstract?

informative abstract


What is an Interface of .Net can you please give an example?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature, just the definition of the methods without the declaration(body). As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.


Give you a sentence of abstract?

The metadata should include an abstract of the article.


What is the abstract noun of give?

giving


You give your daughter 7 acres with a Quit Claim deed now she is selling the land the owner wants an abstract title do you have to give the abstract company your whole abstract to do it?

NAHHH..she can do wateva she likes!!!!!!


What was the purpose of affirmative action?

to give women and minorities preference in school admissions and Job Applications


What is the reasoning behind the ridiculous user interface of wikianswerscom?

could you give an example of a ridiculous part of the interface. Thanks.


What is the purpose for an abstract?

The purpose of an abstract is to give the reader a synopsis of what they are about to read. The abstract generally provides the details of the research including how the research was conducted.


What is the abstract noun of verb give?

The abstract noun form of the verb to give is the gerund, giving. The noun 'give' is an abstract noun as a word for the capacity or inclination to yield under pressure. The noun 'give' is a concrete noun as a word for the elasticity of something that can be stretched or springs back from physical pressure.


What is a sentence with abstract in it?

Professional journals give a few paragraphs as an abstract of an article.The abstract artwork was bright and interesting. Our teacher taught us the difference between abstract and realism in art.