New Delhi, India
minigranth@gmail.com

Abstraction In Java

Created with Sketch.

Core Java Tutorial

[fblike]

Abstraction In Java : Introduction

  • Abstraction in Java is an OOPs concept. Abstraction refers to hiding the implementation details and showing only necessary information to the world.
  • Here we will discuss the ways in which abstraction is achieved in Java.
  • In Java abstraction can be achieved by two ways :
    1. Abstract class
    2. Interface
  • We will discuss both with examples.

 

Abstraction In Java : The Abstract Class

  • In Java a class preceded by abstract keyword is called abstract class.
  • In an abstract class the methods which are declare abstract are called abstract methods. Abstract methods are only declared not defined.

 

Syntax

abstract type method_name(parameter/no parameter)

 

  • Abstract class may contain abstract or non – abstract methods hence an abstract class does not provide full abstraction.
  • Abstract class need to be inherited by other class (extended) and its abstract methods need to be overridden. Hence in abstract class only the abstract methods are overridden.
  • Abstract class can contain constructors and static methods too but they are not declared abstract.
  • Any class containing one or more abstract method need to be declared abstract.

 

Note : Remember that abstract class cannot be instantiated , i.e. we can’t create an object of an abstract class , but can only create a reference variable to refer to other class object which is helpful in run-time polymorphism.

 

Example

 

This image describes the sample program supporting abstract class and abstraction in java.

Abstract Class : Example

 

Output

 

This image describes the output of sample program supporting abstract class and abstraction in java

Abstract Class : Output

 

Abstraction In Java :The Interfaces

  • Interface in Java is the way to achieve complete abstraction. Also interface in Java is used to achieve multiple inheritance by implementing interfaces.
  • Interface is similar to class and contains only public, abstract methods and public, static and final fields. Interfaces differ from class in the way that interface cannot contains constructors and non – abstract methods. Also interfaces cannot be instantiated.
  • Interfaces should be implemented by using implements The class that implements the interface should define all the methods of interface and the method should be declared public.
  • Interface only tells what a class contains and the behaviour is defined in the class that implements it.

 

Syntax to Declare Interface

interface <interface_name> {

       // fields that are public, static and final by default ;

      //methods that are public and abstract by default ;

}

 

Syntax to Implement Interface

class <class_name> implements <interface_name>{

       // implement all the methods of interface ;

      // class fields ;

    // class methods;

}

 

Note-1 A class can implement any number of interfaces , different interfaces name are separated by comma(,)

Syntax

class <class_name> implements <interface 1>,<interface 2>,…..

 

Note-2 : A class can extend another class and implement an interface at the same time.

Syntax

class <class_name> extends <par_class_name> implements <interface_name>

                                                or

class <class_name> implements <interface_name> extends <par_class_name>

 

Note-3 An interface can extend another interface.

 

Interface In Java : Example

 

This image describes a program supporting the concept of interfaces under abstraction in java.

Interfaces In Java : Example

 

Output

 

This image describes output of program supporting the concept of interfaces under abstraction in java.

Interfaces In Java : Output

 

  • In above example we have created an interface which contains an abstract method func1(). The interface is implemented by class A and method func1() is defined publicly.

 

Practical Example on Interface

 

This image describes a practical example supporting the concept of interfaces under abstraction in java.

Interfaces In Java : Example

 

Output

 

This image describes the output of practical example supporting the concept of interfaces under abstraction in java.

Interfaces In Java : Output

 

Abstraction In Java : Multiple Inheritance Using Interface

  • In Java we can achieve multiple inheritance using interface. As we have discussed before multiple inheritance is when one child class inherits from more than one parent class.
  • But in case of interface one class will implement more than one interfaces. Let us take an example :

 

This image describes a program supporting multiple inheritance using interfaces in java.

Multiple Inheritance : Example

 

Output

 

This image describes Output of program supporting multiple inheritance using interfaces in java.

Multiple Inheritance : Output

 

Abstraction In Java : Extending Interfaces

  • As discussed before interfaces can be extended like classes. We can inherit one interface from another by simply using extends keyword as we used to do in inheriting classes. Let’s take an example :

 

This image describes the program supporting extending interfaces in java.

Extending Interfaces : Example

 

Output

 

This image describes the output of program supporting extending interfaces in java.

Extending Interfaces : Output

 

  • In above example there is a multilevel inheritance between interfaces A,B and C. Hence C has all the members of interfaces A and B.
  • So we have implemented interface C only and then all the methods of all the interfaces are defined in class Demo.