Abstraction in Java - Interfaces and Abstract Classes

In plain English, abstract means a concept or idea not associated with any specific instance and does not have a concrete existence. Abstraction in Object Oriented Programming refers to the ability to make a class abstract. Abstraction captures only those details about an object that are relevant to the current perspective, so that the programmer can focus on a few concepts at a time.

Java provides interfaces and abstract classes for describing abstract types.

  • An interface is a contract or specification without any implementation. An interface can't have behavior or state.

  • An abstract class is a class that cannot be instantiated, but has all the properties of a class including constructors. Abstract classes can have state and can be used to provide a skeletal implementation. 

Constructor of an abstract class is invoked by a subclass from its constructor using super keyword (e.g. super()).

 

Defining an interface

An interface can be defined using the interface keyword.

interface MyInterface{

// public static and final fields
// public and abstract methods

}

 

Properties of an Interface

  1. An interface is a contract and they don't have state, behavior or constructors.

  2. Interfaces can have only public and abstract methods.

    • The public and abstract keywords are implicit for interface methods, but can also be declared explicitly.

  3. Interfaces can have static constant fields (public, static and final).

    • Fields of an interface are implicitly public, static and final; though they can be explicitly defined with these modifiers. 

  4. An interface can have another class as a member

    • An inner class of an interface can be marked with public, static, final and abstract; but not final and abstract together.

  5. An interface can have another interface as a member

    • An inner innerface of an interface can be marked with public, static, and abstract.

  6. First concrete subclass should implement all abstract methods.

    • An abstract subclass may or may not implement any methods of an interface

  7. A class can implement an interface using the keyword implements.

    • class A implements I {}

  8. An interface can extend another interface using extends keyword.

    • interface I1 extends I2 {}

  9. An inteface may be marked as abstract explicitely

    1. E.g. abstract interface Inter1{}

  10. Top level interface can be only public or package-private (default)

 

Defining an abstract class

We can define an abstract class using the abstract keyword. 

An abstract class is a class marked as abstract and they cannot be instantiated.

class abstract MyAbstractClass{

  abstract void myAbstractMethod();

  void myConcreteMethod(){

    //method body

  }

}

An abstract method is a method with only signature and no body, whereas a concrete method is a method with body. 

The abstract methods are defined using the abstract keyword and omitting the body and ending the statement with a semicolon.

 

Properties of Abstract Class

  1. Abstract classes cannot be instantiated using the new keyword. 

  2. Abstract classes can have state and can be used to provide a skeletal implementation. 

  3. Abstract classes have all the features of a class, including constructors (except that abstract classes cannot be instantiated using the new keyword).

    • Constructor of an abstract class is invoked by the subclass constructor using super keyword (e.g. super()).

    • Abstract classes can use this pointer.

    • Abstract classes can have private or any access level methods.

    • Abstract classes can also have synchronized methods.

  4. Abstract classes can have abstract and/or concrete methods in any combination.

    • Abstract class can have only concrete methods and no abstract methods.

  5. A concrete class should extend an abstract class (using keyword extends) and implement all abstract methods.

 

A detailed comparison of interfaces and abstract classes can be found at interface-vs-abstract-class.

Comments

if we create Abstract Class we should use inheritence ,

for both Abstract Class and interface we can't create object..

Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!