When we will use an Interface and Abstract class?

Answer Posted / mohd shakir

I think if you have basics knowledge of any OO language then you should be aware of basic definition of Interface and Abstract class.

Here I am going to explain when to use Interface and when to use Abstract class.

Abstract class:-> If you have a common functionality that is used by its subclasses then put that method in super abstract class(now you need not to define that common method in every subclass again and again) This is what you can't do with Interface(because all the methods are by default abstract).

Interface:-> If you don't have the scenario as I discussed above(in abstract class)then choose Interface.And you can also achieve multiple inheritance using Interface that is not possible in case of Abstract class.

Is This Answer Correct ?    21 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is static data type in java?

795


Is a class subclass of itself?

862


What are structs in java?

824


Does constructor be static?

800


Where import statement is used in a java program?

839


What is a class in java?

810


What is a boolean output?

739


Difference between current previous versions of Java?

807


Can a top level class be private or protected?

806


Can we pass a primitive type by reference in java? How

727


What is nextline method in java?

816


What is singletonlist in java?

731


What happens to a static var that is defined within a method of a class?

801


what is thread? What are the high-level thread states? Or what are the states associated in the thread? : Java thread

782


In java, what is the difference between method overloading and method overriding?

833