• Abstraction is fundamental principle of modeling. A system model is created at different levels, starting at the higher levels and adding more levels with more details as more is understood about the system. When complete, the model can be viewed at several levels.
  • So abstraction is about,
  • Looking only at the information that is relevant at that time
  • Hiding details so as not to confuse the bigger picture


  • Abstraction is a process of developing a class by hiding or removing non essential details relevant to user. here non essential details means method body and logic 
  • Basically Abstraction provides a contract between a service provider and clients.
  • we can achieve this by using abstract class

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • Abstract class can not be instantiated directly.
  • Means we can not create object for abstract class directly

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9.  
  10. public static void main(String[] args){
  11.  
  12. AbstractDemo   obj= new AbstractDemo(); 
  13. // ERROR: Cannot instantiate the type AbstractDemo
  14.  
  15. }
  16. }

Can we declare abstract methods as static?

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     static abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.

Can we declare abstract methods as final?


  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.
  • Because it should be inherited to sub class.


Can we declare abstract methods as private?

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.
  • Because it should be inherited to sub class.

 Legal modifiers allowed in combination with abstract

 1.native

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     native abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 2.protected

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     protected abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 3.public

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.    public abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }



 4.default

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.   abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }


How it works:

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.   abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }


  1. package Abstraction;
  2. public abstract class Example extends AbstractDemo {
  3.  
  4.   abstract void add(){
  5.   System.out.println("this is implemented method in sub class");
  6. }
  7.  
  8.  
  9. }

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

2 comments for Abstraction

  1. what is the role of the default constructor why it's differ from no argument constructor?

    ReplyDelete
  2. Great samples!!
    Good luck in the future.

    ReplyDelete

Select Menu