Quick Contact


    Difference between abstract class and interface

    As we know that, use of abstract class and interface we can achieve abstraction and declare the abstract methods. But still there are many differences between abstract class and interface.

    Interface Abstract class
    Interface support multiple implementations Abstract class does not support multiple implementation
    Interface does not contain data member Abstract class contains data member
    Interface does not contain Constructor Abstract class contains Constructor
    An Interface contains only incomplete member (signature of member) An Abstract class contains both incomplete (abstract) and complete member
    An interface cannot have access modifier by default everything id assumed as public An Abstract class contains access modifiers for the subs, functions, properties.
    Member of Interface cannot be static Only Complete Member of abstract class can be Static

    Example of Interface and Abstract class in Java

    Here is the example of using Interface and abstract class both

    		//Creating interface which have 4 methods  
    		interface Animals{  
    		void dog();//bydefault, public and abstract  
    		void cat();  
    		void pig();  
    		void lion();  
    		//Creating abstract class that provides the implementation of one 
    		method of Animals interface  
    		abstract class Voice implements Animals{  
    		public void pig(){System.out.println("Pig voice: wee wee");}  
    		//Creating subclass of abstract class, now we need to provide the 
    		implementation of rest of the methods  
    		class Voices extends Voice{  
    		public void dog(){System.out.println("Dog voice: Bark Bark");}  
    		public void cat(){System.out.println("Cat voice: Mew Mew");}  
    		public void lion(){System.out.println("Lion voice: Roar");}  
    		//Creating a Demo class that calls the methods of Animals interface  
    		class Demo{  
    		public static void main(String args[]){  
    		Animals a=new Voices();  


    Dog voice: Bark Bark

    Cat voice: Mew Mew

    Pig voice: wee wee

    Lion voice: Roar

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.