Monday, January 16, 2017

Java Difference between Abstract class and Interface

Abtract class and interface both are used to achieve abstraction.
In this post, I will show you Diffence between Abstract class and Interface.

For example
// Create interface with 3 methods
public interface A{

 void a();
 void b();
 void c();

// creating abstract class that provides the implementation of one method of A interface
abstract class B implements A {
public void c(){
    System.out.println("Hello C");

// creating subclass of abstract class, now we need to provide the implementation of 
rest of the method
class H extends B{
    public void a() {System.out.println("Hello A")};
    public void b() {System.out.println("Hello B")};
    public void c() {System.out.println("Hello C")}

// creating a test class that calls the methods of A interface
class Test {
public static void main(String args[]) {
    A a = new H();
Result : 
Hello A
Hello B
hello C
Good luck!


Post a Comment

Total Pageviews