what is difference between abstract class and interface in java

In this tutorial we will learn what is the significance of  Abstract class and Interface  and what is the  difference between abstract class and interface in java.

In java basically to implement/achieve the one of powerful feature “abstraction” we need use either Abstract class and Interface, both have abstract methods. There are some cases when we need to use interface and abstract class in Java.

what is an Interface in Java.

Interface looks same as class but it is not a class, Interface can have  method declarations and variables and classes. The methods declared in Interface are abstract by default and the variables are public,static,final by default.

 

What is abstarct class in Java

The class which is declared with abstract key word is know as abstract class. If a class contained at least one abstract method then we should declare that class as abstract. We declare a class as abstract even there is no abstract methods.

 

Difference between Abstract class and Interface.

Abstract Class Interface
Abstract class declares with abstract Keyword Interface declares with interface keyword
Abstract class can able extend only one class or one abstract class at a time Interface can able extend any number of interfaces at a time
Abstract class can have both abstract and non-abstract methods Interface can have only abstract methods by default
Abstract class can have public,protected,public abstract methods,static methods,main method and constructor Interface can’t have static methods, main method or constructor ,can have only public abstract methods
Abstract class can have static,final or static final variable with any access specifier Interface can have only static final variable i.e. by default
Abstract class can extend from a class or from an abstract class Interface can extend only from an interface and implements from a class

 

Happy learning Java Tutorial.. :)

 

Posted in InterviewQuestions and tagged , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">