Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Difference between interface and abstract class?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Difference between interface and abstract class?

    Hi friends this is one of most java interview question.So, we will see clear difference between
    abstract class and interface.

    you may visit this link to see clear view

    Interface:-


    1) If we don't know any thing about implementation just we have requirement specification then we should go for interface.

    2) Every method present inside interface is by default public & abstract

    3) The following modifiers are not allowed for interface methods, those are:
    stritfp, protected,static,native,private,final,synchronized


    4) Every variable present inside interface is public, static final , bydefault wheather we are declare or not.

    5) For the interface variables we can't declare the following modifiers, those are:
    private, protected, transient, volatile

    6) For the interface variables compulsary we should perform initialization at the time of declaration only

    7) Inside interface we can't take instance& static blocks

    8) Inside interface we can't take constructor.

    Abstract Class:-


    1) If we are talking about implementation but not completly(partial implementation) then we should go
    for abstract class


    2) Every method present inside abstract class need not be public & abstract. we can take concrete
    methods also.

    3) There are no restrictions for abstract class method modifier i.e, we can use any modifier

    4) Abstract class variables need not be public, final static.

    5) There are no restriction for abstract class variable modifiers

    6) For the abstract class variables there is no restriction like performing initialization at the time of declaration.

    7) Inside abstract class we can take static block & instance blocks

    8) Inside abstract class we can take constructor.
    Last edited by copeg; January 11th, 2014 at 02:37 PM. Reason: link removed


  2. #2
    Junior Member
    Join Date
    Jul 2018
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between interface and abstract class?

    What is the difference between interface and abstract class in Java? is the most frequently asked question in Java Interview and it is one of the toughest question to answer. After the introduction of Java 8 this has become quite tougher because the interfaces for both static and default methods can be included in the default methods, in this new version.

    There are some interview panel members who also check for the practical experience of the candidates on when to use interface in Java rather than just focusing on the difference. This is very tough to answer and one must have a very good knowledge about interface and abstract class in Java. Therefore, you need to keep in mind that if you are preparing for Java Programming Interview then you also refer a book of Java concepts named as Java Programming Interview Exposed. In it, there are lots of important questions and topics included Eg: multi threading, OOPS concepts, collections, frameworks similar to Hibernate and Spring, data structure, unit testing and algorithms apart from that recent technology questions like Scala, Android, and other JVM languages.

    Read More : What is an Abstract Class in Java?

    Now, let us check out some difference between abstract class and interface in Java programming language.

    Abstract class vs Interface in Java

    1) An Interface is an interface and an abstract class is a class and this is the major difference between abstract class and an interface which implies that by extending the abstract class it is not possible to extend another class as Java does not support multiple inheritances but it is possible to implement multiple inheritance in Java.

    2) It is not possible to create a non-abstract method in an interface, is the second difference between interface and abstract class and it is by default abstract. You can develop a non-abstract method in abstract class. With the help of abstract keyword a class which does not contain any abstract method can be made abstract.

    Read More : Significance Of Abstract Methods and Classes In Java

    3) For type declaration interface is best suited and for reusing the code and evolution perspective abstract class is used. Why there is a need to use interface for type declaration can be explained by the book “Effective Java” very well.

    4) Abstract class are faster when compared to interface because before calling any overridden method in Java there is a search carried out by the interface. Although it is of not that importance it works well in time critical application.

    Read More : What is Java Interface And how does it Work?

    5) If a new method is added in an existing interface then the interface which is present breaks all the implementation and it is necessary to implement all the clients that is not advisable. A default implementation can be provided with the help of an abstract class for a new method without breaking the current clients.

  3. #3
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 9 Times in 6 Posts

    Default Re: Difference between interface and abstract class?

    abstract class vs Interface in Java

    1) An Interface is an interface and an abstract class is a class and this is the major difference between abstract class and an interface which implies that by extending the abstract class it is not possible to extend another class as Java does not support multiple inheritances but it is possible to implement multiple inheritance in Java.

    2) It is not possible to create a non-abstract method in an interface, is the second difference between interface and abstract class and it is by default abstract. You can develop a non-abstract method in abstract class. With the help of abstract keyword a class which does not contain any abstract method can be made abstract.


    3) For type declaration interface is best suited and for reusing the code and evolution perspective abstract class is used. Why there is a need to use interface for type declaration can be explained by the book “Effective Java” very well.

    4) Abstract class are faster when compared to interface because before calling any overridden method in Java there is a search carried out by the interface. Although it is of not that importance it works well in time critical application.


    5) If a new method is added in an existing interface then the interface which is present breaks all the implementation and it is necessary to implement all the clients that is not advisable. A default implementation can be provided with the help of an abstract class for a new method without breaking the current clients.

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Difference between interface and abstract class?

    I don't like responding to old threads but subsequent replies are repeating some things that have either changed or were initially incorrect. As of Java 8, interfaces may take both fully defined static methods and default methods. And abstract methods within an abstract class may only be modified with either public or protected.

    Regards,
    Jim

  5. #5
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 9 Times in 6 Posts

    Default Re: Difference between interface and abstract class?

    Abstract class vs Interface

    Type of methods: Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.
    Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
    Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
    Implementation: Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
    Inheritance vs Abstraction: A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”.
    Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
    Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.
    Abstract Class vs Interface

  6. #6
    Junior Member
    Join Date
    Oct 2018
    Location
    Noida
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difference between interface and abstract class?

    First of all, we should know what is Abstraction, Abstraction is Hiding the internal implementation of the feature and only showing the functionality to the users. i.e. what it works (will show) and how it will works (hide the implementation).
    Abstract class and interface both are used for Abstraction.
    Some major differences I am explaining between Interface And Abstract-

    1. Interface can have only abstract methods but Abstract class can have abstract and non-abstract methods.
    2. An abstract class can have final, non-final, static and non-static variables but Interface has only static and final variables
    3. Variables declared in a Java interface are by default final and An abstract class may contain non-final variables.
    4. An abstract class can provide the implementation of an interface. An interface can’t provide the implementation of an abstract class.
    5. Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.
    Regards
    Divya

  7. #7
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 9 Times in 6 Posts

    Default Re: Difference between interface and abstract class?

    Abstraction: Hiding the internal implementation of the feature and only showing the functionality to the users. i.e. what it works (showing), how it works (hiding). Both abstract class and interface are used for abstraction.

    Abstract class vs Interface

    Type of methods: Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.
    Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
    Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
    Implementation: Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
    Inheritance vs Abstraction: A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”.
    Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
    Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

  8. #8

    Default Re: Difference between interface and abstract class?

    abstract keyword is used to create an abstract class and it can be used with methods also whereas interface keyword is used to create interface and it can’t be used with methods.

    Sub-classes use extends keyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class unless the subclass is also an abstract class whereas sub-classes use implements keyword to implement interfaces and should provide implementation for all the methods declared in the interface.

    Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations.
    Abstract classes can have constructors but interfaces can’t have constructors.

    Abstract class have all the features of a normal java class except that we can’t instantiate it. We can use abstract keyword to make a class abstract but interfaces are a completely different type and can have only public static final constants and method declarations.

    Abstract classes methods can have access modifiers as public, private, protected, static but interface methods are implicitly public and abstract, we can’t use any other access modifiers with interface methods.

    A subclass can extend only one abstract class but it can implement multiple interfaces.

    Abstract classes can extend other class and implement interfaces but interface can only extend other interfaces.

    We can run an abstract class if it has main() method but we can’t run an interface because they can’t have main method implementation.

    Interfaces are used to define contract for the subclasses whereas abstract class also define contract but it can provide other methods implementations for subclasses to use.

Similar Threads

  1. Java Abstract class and Interface issues in a code
    By John234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 09:51 PM
  2. [SOLVED] Abstract method in a non-abstract class
    By bassie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 2nd, 2012, 11:27 AM
  3. abstract VS interface
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2012, 01:07 PM
  4. [SOLVED] Sahaja here. Need guidance. Interface and Abstract class
    By sahaja in forum Java Theory & Questions
    Replies: 2
    Last Post: July 4th, 2012, 04:43 PM
  5. What's difference between public class and abstract class?
    By Java95 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 24th, 2012, 07:37 AM

Tags for this Thread