Friday, March 10, 2017

Abstraction in Java






Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don’t know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)

Abstract class:

If a class contain any abstract method then the class is declared as abstract class. An abstract class is never instantiated. It is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method.
Syntax :
abstract class class_name { }

Abstract method:

Method that are declared without any body within an abstract class are called abstract method. The method body will be defined by its subclass. Abstract method can never be final and static. Any class that extends an abstract class must implement all the abstract methods declared by the super class.
Syntax :
abstract return_type function_name ();    // No definition
Example of abstract class that has abstract method-
In this example, Bike the abstract class that contains only one abstract method run. It implementation is provided by the Honda class.
abstract class Bike

     abstract void run(); 

class Honda4 extends Bike

     void run()
   {
     System.out.println(“running safely..”);
   } 
public static void main(String args[])
   { 
     Bike obj = new Honda4(); 
     obj.run(); 
   } 

Output:
running safely..

Q. When to use Abstract Methods & Abstract Class?

Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.
Abstract classes are used to define generic types of behaviors at the top of an object-oriented programming class hierarchy, and use its subclasses to provide implementation details of the abstract class.

Important Points about Abstract classes and methods:

  1. abstract keyword is used to create an abstract class in java.
  2. Abstract class in java can’t be instantiated.
  3. We can useabstract keyword to create an abstract method, an abstract method doesn’t have body.
  4. If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile.
  5. It’s not necessary to have abstract class to have abstract method.
  6. If abstract class doesn’t have any method implementation, it’s better to use interface because java doesn’t support multiple class inheritance.
  7. The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.
  8. All the methods in an interface are implicitly abstract unless the interface methods are static or default. Static methods and default methods in interfaces are added in Java 8
  9. Java Abstract class can implement interfaces without even providing the implementation of interface methods.
  10. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.
  11. We can run abstract class in java like any other class if it has main()

No comments:

Post a Comment