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?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)