CodesJava

Easy learning with example program codes

what is difference between abstract class and interface in java?


Abstract class in real world:

Let us take an example of graphic objects. Different graphic objects are there such as circle, rectangle, triangle etc. They all have state defined by their positions, colour etc. and behaviour defined by draw, resize, calculate size etc. As all these object types has common things but with different implementations. We can take advantage of common things with different implementations and put these common things in an abstract class (say GraphicObjects) then extends this class in subclasses to provide specific implementations.

Abstract class in java:

Abstract class is a way of implementing 0 to 100% abstraction. A class declared with abstract keyword is known as an abstract class. An abstract class may or may not contain abstract method. Abstract classes cannot be instantiated.

Syntax:

abstract class className {

   // declare fields

   // declare abstract/non-abstract methods

}

 

Interface in real world:

You can see number of interface examples. Let us take example of a TV. You press change channel button of TV remote and channel is changed. Here remote is act as an interface between you and TV.

Dictionary meaning of interface:

A point where two systems, subjects, organizations, etc., meet and interact.

Interface in java:

Interface is a way of implementing 100% abstraction. An interface is declared with interface keyword. It can contain only abstract methods and static final data members Or Interface is a group of related abstract methods.

Syntax:

Interface Interface_Name {

                //abstract methods

                //static final data members

}

 

Abstract class Interface
It can have method body (non-abstract methods). It have only abstract methods.
It can have instance variables. It cannot have instance variables.
It can have constructor. It cannot have constructor.
It can have static methods. It cannot have static methods.
You can extends one abstract class. You can implement multiple interfaces.

 

Java interview questions on abstract class

  • What is abstract class in java?
  • Why abstract class is used in java?
  • Can abstract class have constructors in java?
  • Can abstract class be final in java?
  • Can we declare local inner class as abstract?
  • What is difference between abstract class and interface in java?
  • Sign Up/ Sign In
    Ask a Question


    Industrial Training

    We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

    Development

    We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

    Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki