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.
1 2 3 4 5 6 7 8 9 10 11 |
interface school { String bangalore_code ="SCTC"; // variable void test(); // no body } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
abstract class school { abstract void test(); void test1(){ //some logic ..... } } |
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.. 🙂