Saturday, February 27, 2016

Abstraction in Java

What is abstraction? Abstraction is the concept of segregate the implementation of interface. It will focus only on essential details instead of showing the full implementation details. Abstraction can be applied through abstract class and interface.
Abstract class is something which is incomplete and provide common information for the subclasses. You cannot create instance for abstract class. If you want to use it you need to make it complete or concrete by extending it.
When do you use abstraction? When you know something needs to be there but not sure how exactly it should like.
Abstraction key points
  • Use abstraction if you know something needs to be in class but implementation of that varies. 
  • You cannot create instance of abstract class using new operator. 
  •  Abstract class can have constructor 
  •  A class automatically become abstract class when any of its method declared as abstract. 
  •  Abstract method does not have method body 
  •  Variable cannot be made abstract. Its only for class and methods 
  •  If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be concrete class.

No comments: