Java Abstraction | Java example

 

Java Abstraction :

Java Abstraction is a part of OOP concept which takes care of hiding the underlying definition of methods.We can get to know in detail in this part of tutorial.

abstract keyword is used to achieve abstraction in java.

 

Object Creation:

Object of a class is create to make use of the properties declared in the class.

Example:

Here

–> ABC is a class

–> abc is object

–> ABC() is constructor

ABC abc = new ABC();
abc.(access the methods of class abc)

 

Abstract Methods:

Any class consists of only declaration of method and hiding its implementation such methods are called to be abstract methods.

We can create a abstract method by using abstract keyword.

Java Abstraction

abstract void display();

 

Abstract Class:

If any class consist of at-least one abstract method then its called as abstract class. Abstract class may have abstract methods and general methods.

We can create a abstract class only by making it abstract.

Java Abstraction

 

So now the methods are declared but where we will implement them???.

We can implement them in derived class using inheritance concept.

For abstract classes we cannot create object.

 

abstract class AbstractExample {

    abstract void display();
}

 

Try to create a object for this class if you want to get a clear idea of what is abstract class.

 

Java Abstraction

Concrete Class:

A class having fully defined methods within them are said to be concrete class.Which means i have declare few methods and provided their implementations in the class itself then we can consider them to be concrete class.

For concrete class we can create a object.

class ConcreteClass extends abstract_example{

    @Override
    void display(){

        System.out.println("abstract method declaration");
    }
}

 

object can be created in this case and access method.

ConcreteClass concreteClass = new ConcreteClass();
concreteClass.display();

 

As we cannot create a object for abstract class we can make use of concrete class for implementation.

 

class Impl{

    public static void main(String args[]){

        ConcreteClass concreteClass = new ConcreteClass();
        concreteClass.display();

    }
}

Java Abstraction

 

 

Show Buttons
Hide Buttons